loop through columns of data frame in r -
i have following problem:
levelsvar <- c("arrears", "expenses", "warmhome", "telephone", "colortv", "washer", "car", "meatfish", "holiday") variables <- null (i in 1:length(levelsvar)) { variables <- sapply(levelstest, function(x) (length(test$levelsvar[i][test$country==x & test$levelsvar[i]=="1"]) + length(test$levelsvar[i][test$country==x & test$levelsvar[i]=="2"])) / length(test$levelsvar[i][test$country==x])) }
i want use loop perform function can see above 9 times levels of "levelsvar". tried various times failed. think problem r reads
test$"arrears"
instead of
test$arrears
i tried use noquote() didn't help.
do have solution problem?
thank in advance!
edit:
with example
levelstest <- c("at", "be") levelsvar <- c("arrears", "expenses", "warmhome", "telephone", "colortv", "washer", "car", "meatfish", "holiday") structure(list(country = c("at", "at", "at", "be", "be", "be" ), arrears = c(1l, 1l, 1l, 2l, 1l, 1l), expenses = c(3l, 1l, 3l, 1l, 1l, 2l), warmhome = c(1l, 2l, 2l, 1l, 1l, 1l), telephone = c(4l, 1l, 4l, 4l, 3l, 3l), colortv = c(2l, 1l, 3l, 4l, 3l, 1l), washer = c(4l, 1l, 3l, 3l, 1l, 2l), car = c(4l, 4l, 4l, 4l, 3l, 2l), meatfish = c(2l, 1l, 1l, 4l, 1l, 1l), holiday = c(2l, 2l, 1l, 3l, 4l, 2l)), .names = c("country", "arrears", "expenses", "warmhome", "telephone", "colortv", "washer", "car", "meatfish", "holiday"), row.names = c(na, 6l), class = "data.frame")
now tried
variables <- null (i in 1:length(levelsvar)) { variables <- sapply(levelstest, function(x) (length(test[levelsvar[i]][test$country==x & test[levelsvar[i]]=="1"]) + length(test[levelsvar[i]][test$country==x & test[levelsvar[i]]=="2"])) / length(test[levelsvar[i]][test$country==x])) }
but doesn't work.
what wanted achieve percentage (length(test$arrears[test$country==x & test$arrears=="1"]) + length(test$arrears[test$country==x & test$arrears=="2"])) / length(test$arrears[test$country==x]))
levels of levelsvar
(with values 1 , 2) , countries in levelstest
.
the solution problem following:
test <- (structure(list(country = c("at", "at", "at", "be", "be", "be" ), arrears = c(1l, 1l, 1l, 2l, 1l, 1l), expenses = c(3l, 1l, 3l, 1l, 1l, 2l), warmhome = c(1l, 2l, 2l, 1l, 1l, 1l), telephone = c(4l, 1l, 4l, 4l, 3l, 3l), colortv = c(2l, 1l, 3l, 4l, 3l, 1l), washer = c(4l, 1l, 3l, 3l, 1l, 2l), car = c(4l, 4l, 4l, 4l, 3l, 2l), meatfish = c(2l, 1l, 1l, 4l, 1l, 1l), holiday = c(2l, 2l, 1l, 3l, 4l, 2l)), .names = c("country", "arrears", "expenses", "warmhome", "telephone", "colortv", "washer", "car", "meatfish", "holiday"), row.names = c(na, 6l), class = "data.frame")) levelsvar <- c("arrears", "expenses", "warmhome", "telephone", "colortv", "washer", "car", "meatfish", "holiday") levelstest <- c("at", "be") variables <- null (i in 1:length(levelsvar)) { variables <- cbind(variables, sapply(levelstest, function(x) (length(test[levelsvar[i]][test[1]==x & test[levelsvar[i]]=="1"]) + length(test[levelsvar[i]][test[1]==x & test[levelsvar[i]]=="2"])) / length(test[levelsvar[i]][test[1]==x]))) }
Comments
Post a Comment