我有以下问题:
levelsvar <- c("arrears", "expenses", "warmhome", "telephone", "colorTV", "washer", "car", "meatfish", "holiday")
variables <- NULL
for (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]))
}
我想使用for循环执行上面9级所有“levelsvar”级别的功能。我尝试了不同的次数,但我失败了。我认为问题是r读取
test$"arrears"
而不是
test$arrears
我已经尝试使用noquote()但它没有用。
你有解决这个问题的方法吗?
提前谢谢!
编辑:
以示例
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")
现在我试过
variables <- NULL
for (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]))
}
但这不起作用。
答案 0 :(得分:0)
我想要达到的目标是获得所有(length(test$arrears[test$country==x & test$arrears=="1"]) + length(test$arrears[test$country==x & test$arrears=="2"])) / length(test$arrears[test$country==x]))
级别(值1和2)以及levelsvar
所有国家/地区levelstest
的百分比。
我的问题的解决方案如下:
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
for (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])))
}
答案 1 :(得分:0)
你需要的只是测试而且:
package com.company.xxx.junit;
带有margin = 2的apply()将沿着您的列,tapply()将根据分组(国家/地区)计算自定义函数。它甚至可以保存您的变量名称。 test [-1]将跳过国家/地区列。