Error using aggregate to find length with missing values

I am trying to use the aggregate function in R to summarise a data using the length function. My data has some NA's and I have tried using 'na.rm = T' or 'na.omit' however none sees to work. I keep getting this error

'Error in FUN(X[[i]], ...) : 
  2 arguments passed to 'length' which requires 1'
data10 <- structure(list(Group = c(1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 2, 2, 
1, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 
1), SUBJECT = c(1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 10, 
11, 12, 14, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 
23, 24, 25), test = c(1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 
1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 1
), trial = c(1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 
1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3), Condition = c(1, 
2, 3, 1, 3, 1, 2, 3, 2, 3, 1, 2, 1, 2, 3, 1, 3, 1, 2, 3, 2, 3, 
1, 2, 1, 2, 3, 1, 3, 1, 2, 3, 2, 3), Sac2 = c(1, 1, 1, NA, 2, 
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 
1, 1, 1, 1, 2, 2, 1, 1), Sac = c(1, 1, 1, NA, 3, 1, 1, 1, 1, 3, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 1, 1, 1, 7, 1, 1, 1, 1, 1, 1, 3, 
3, 1, 1), Saccade...8 = c(1, 1, 1, NA, 2, 1, 1, 1, 1, 2, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 
1), T_APPEAR = c(9.236, 17.85, 28.942, 63.724, 9.463, 22.963, 
52.068, 57.021, 15.344, 19.783, 37.825, 46.17, 4.339, 21.241, 
29.179, 31.823, 12.164, 22.84, 23.954, 73.663, 27.269, 22.131, 
30.361, 62.674, 6.928, 16.413, 47.555, 48.893, 7.291, 15.796, 
31.788, 54.946, 10.117, 28.83)), row.names = c(NA, -34L), class = c("tbl_df", 
"tbl", "data.frame"))
data14 = aggregate(data10,
                   by = list(data10$SUBJECT,data10$Condition, data10$Group, data10$test),
                   FUN = length(), na.rm=TRUE)

This does not throw and error but I am not sure it does what you want. It returns the size of each group. Is that what you want or do you want the number of non-NA elements?

data14 = aggregate(data10,
                   by = list(Sub=data10$SUBJECT,Cond=data10$Condition, 
                             Grp=data10$Group, Test=data10$test),
                   FUN = length)

Because it is counting the rows with NA's too

The length function does not ignore NA and does not have an na.rm argument. You can write a function to count the non-NA elements in a column.

data14 = aggregate(data10,
                   by = list(Sub=data10$SUBJECT,Cond=data10$Condition, 
                             Grp=data10$Group, Test=data10$test),
                   FUN = function(V) sum(!is.na(V)))

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.