背景:我有一个数据框,其中一列具有重复值。我试图通过挑选具有重复列值的所有行来拆分此数据框,处理它们,然后吐出一个包含所有已处理行的新数据框。
我很惊讶以下代码中出了什么问题:
dataSet <- structure(list(DAY = structure(1:10, .Label = c("Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday"), class = "factor"),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("act1", "act2", "act3", "act4",
"act5", "act12", "act19", "act116", "act22",
"act6", "act13", "act111", "act117", "act23",
"act7", "act14", "act112", "act118", "act24",
"act8", "act15", "act113", "act119", "act25",
"act9", "act16", "act114", "act20", "act26",
"act10", "act17", "act115", "act21", "act27",
"act11", "act18"), class = "factor"), value = c(67,
65, 40, 79, 106, 90, 57, 59, 2, 12)), .Names = c("DAY",
"variable", "value"), row.names = c(NA, 10L), class = "data.frame")
uniq <- unique(dataSet$variable)
for (i in 1:length(uniq)){
rowsPerVal <- dataSet[dataSet$variable == uniq[i], ]
print(length(rowsPerVal))
}
我只是不明白最终的打印语句如何说长度为 3,当数据框中有 10 条记录具有相同的variable
列值时。