Error in data.frame, please Help!

Hi there!

I am totally new with R studio.

I wanted to count all the behaviours I have recorded to make a nice plot but I am getting the following error:

Error in data.frame(treatment, incTime, count.o, count.gb, count.cann, :
arguments imply differing number of rows: 1, 25, 0

This is the code I am using:

column.count.df <- data.frame(Treatment = character(),
                              IncubationTime = character(), 
                              count.o = integer(),
                              count.gb = integer(),
                              count.cann = integer(),
                              count.bu = integer(),
                              count.rem = integer (),
                              count.totalgroomers = integer(),
                              numFocals = integer(),
                              scanTime = double())

TIME_POINTS <- c(12, 15, 18, 21, 24)
TREATMENTS <- c("0% Blastospores-","100% P. entomophila","Ringer", "40% P. entomophila", "40% Blastospores-", "100% Blastospores+")

for (incTime in TIME_POINTS) {
  for ( treatment in TREATMENTS) {
    count.o <- apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                     2, function(x) length(which(x=="o")))
    
    count.cann <- apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                        2, function(x) length(which(x=="c")))
    
    count.bu <- apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                      2, function(x) length(which(x=="bu")))
    
    count.rem <- apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                      2, function(x) length(which(x=="r")))
    
    count.gb <- apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                      2, function(x) length(which(grepl("gb", x))))
    
    count.totalgroomers <- sapply(apply(subset(behavData, data$Time.point == incTime & data$Treatment == treatment), 
                                        2, function(x) strtoi(substring(grep("gb", x, value = TRUE), 3,3))), 
                                  function (x) sum(x))
    
        colData <- data.frame(treatment,
                          incTime,
                          count.o, 
                          count.gb,
                          count.cann,
                          count.bu, 
                          count.rem,
                          count.totalgroomers,
                          numFocals = count.o + count.cann + count.rem + count.gb + count.bu, # non-visible termites don't count
                          seq(0, 2, 1/12))
    names(colData) <- names(column.count.df)
    
    column.count.df <- rbind(column.count.df, colData)
  }
}

So in general the error is coming from the colData.

I would really appreciate if you can help me.

Best regards,

Alejandra

Hi, and welcome!

Thanks for using the FAQ: What's a reproducible example (`reprex`) and how do I do one? but we're missing a key requirement, behavData

You might be able to solve the problem by populating column.count.df, which starts off with zero rows.

column.count.df <- structure(list(Treatment = structure(c(1L, 3L, 6L, 5L, 4L, 2L
), .Label = c("0% Blastospores-", "100% Blastospores+", "100% P. entomophila", 
"40% Blastospores-", "40% P. entomophila", "Ringer"), class = "factor"), 
    IncumbationTimes = c(9, 12, 15, 18, 21, 24), count.o = c(0, 0, 
    0, 0, 0, 0), count.gb = c(0, 0, 0, 0, 0, 0), count.cann = c(0, 
    0, 0, 0, 0, 0), count.bu = c(0, 0, 0, 0, 0, 0), count.rem = c(0, 
    0, 0, 0, 0, 0), count.totalgroomers = c(0, 0, 0, 0, 0, 0), 
    numFocals = c(0, 0, 0, 0, 0, 0), scanTime = c(0, 0, 0, 0, 
    0, 0)), class = "data.frame", row.names = c(NA, -6L))

After you run the code and get the error, examine the vectors used to make the colData data frame. Make sure each matches what you assume to be true.

data.frame will not work if one of the vectors given isn't a divisor of another vector. In your case, one of the vectors is length 0 and the others have a positive length. If this is alright with what you're planning, then you'll need to explicitly handle it.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.