嵌套 for 循环中的数据帧。 <0 行>(或 0 长度 row.names)错误

Dataframes in nested for loop. <0 rows> (or 0-length row.names) error

这是我正在处理的数据框的一个子集:

ID   FRUIT1   FRUIT2   FRUIT3   VEG1   VEG2   VEG3
1      1        2        2       1       2      2
2      2        1        1       1       1      1
3      2        1        2       1       2      2
4      2        2        2       1       2      1 
5      1        1        1       2       1      2

它包含 5 个受试者,其中有关于 3 种水果和 3 种蔬菜的信息:

我想知道有多少人吃了 9 种可能的水果和蔬菜组合(FRUIT1 和 VEG1,FRUIT1 和 VEG2,……)。 这是我写的脚本:

# Read data 
dataframe <- read.csv("myfile.csv", header=TRUE)

# Define variables
FRUIT= names(dataframe)[2:4])
VEG= names(dataframe[5:7]))

# Check frequency of interactions
for (fruit in FRUIT) {
    for (veg in VEG) {
        #Double-positive: keep only subjects that each both the fruit and the vegetable
        PP <- dataframe[dataframe$fruit=='2' & dataframe$veg=='2',]
        #Double-negative: keep only subjects that don’t eat any 
        AA <- dataframe[dataframe$fruit=='1' & dataframe$veg=='1',]
        #Only FRUIT-positive: keep only subjects that eat the fruit, but not the vegetable
        PA <- dataframe[dataframe$fruit=='2' & dataframe$veg=='1',]
        #Only VEG-positive: keep only the subject that eat the vegetable, but not the fruit
        AP <- dataframe[dataframe$fruit=='1' & dataframe$veg=='2',]
        # Print the name of the fruit, the vegetable, and the counts of each of the 4 categories 
    toprint <- c(kir,hla,nrow(PP),nrow(AP),nrow(PA),nrow(AA))
    setwd(“~/Directory/“)
    write(toprint, file = "NumberIndividuals.csv",ncolumns=6,append = TRUE, sep = " ")
    }
}

问题:上面的脚本在 for 循环之外工作,但在这个嵌套的 for 循环中我收到以下消息:<0 rows> (or 0-length row.names) for PP、AA、PA 和 AP .为什么在这种情况下子数据集(PP、AA、PA 和 AP)为空?

您可以在没有显式 for 循环的情况下尝试此操作:

combos<-expand.grid(fruit=grep("FRUIT",colnames(dataframe),value=TRUE),
                    veg=grep("VEG",colnames(dataframe),value=TRUE),
                    stringsAsFactors=FALSE)
counts<-apply(combos,1,function(x) sum(rowSums(dataframe[,x]==2)==2))
cbind(combos,counts=counts)
#   fruit  veg counts
#1 FRUIT1 VEG1      0
#2 FRUIT2 VEG1      0
#3 FRUIT3 VEG1      0
#4 FRUIT1 VEG2      2
#5 FRUIT2 VEG2      2
#6 FRUIT3 VEG2      3
#7 FRUIT1 VEG3      1
#8 FRUIT2 VEG3      1
#9 FRUIT3 VEG3      2

你需要改成PP <- dataframe[dataframe[[fruit]] == '2' & dataframe[[veg]] == '2',],其他的也一样,fruit是字符串,dataframe$fruit不是列