It's always best to do this with list operations (e.g. lapply) rather
than a loop:
DF1 <- split(DF, DF$male)
DF2 <- lapply(DF1, function(x) {
x2 <- t(as.matrix(x[3:5], dimnames = list(levels(x$age), NULL)))
as.data.frame(x2)
})
Then DF2[["0"]] and DF2[["1"]] are the data.frames you want.
HTH
Dear R-listers,
I am trying to assign colnames to a data frame within a loop, but I keep
getting a "target of assignment expands to non-language object"-error. I
need to split up a large dataset into about 20 smaller ones, and I would
like to assign colnames within the loop, so I won't have to typ
2 matches
Mail list logo