nzaleski
nzaleski

Reputation: 441

Subsetting Factors in R

s <- split (x, x$factor)
s[[1]]

So I would like to be able to use that subset of the factors with lapply. Below is a mock code simulated what I was trying to do.

meanfunction <-lapply(s[[1]],function(x) colMeans(x[, c("col1","col2")]))

But it doesn't work. So I though I would be smart and try to trick R and bind a new variable t<-s[[1]] and then use that in the function, but alas it did not work either. Either approach I get the same error message.

Error in x[, c("col1", "col2")] : incorrect number of dimensions

But the code works just fine when evaluated as a whole. Any thoughts? Thanks.

Edit : x is a data.frame with 4 columns Character, numeric, numeric, factor

Upvotes: 0

Views: 275

Answers (1)

dirkchen
dirkchen

Reputation: 195

dplyr is a great choice for this kind of job, supposing I understood your goal correctly. Here is an example:

library(dplyr)
x = tbl_df(data.frame(factor = factor(c(rep("A", 3), rep("B", 4))), value = 1:7))
group_by(x, factor) %>% summarise(mean = mean(value))

You can find more about dplyr here.

Upvotes: 1

Related Questions