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

时间:2016-11-17 10:40:17

标签: r dataframe subset two-columns

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

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种蔬菜的信息:

  • 1 =受试者不吃水果/蔬菜
  • 2 =受试者吃水果/蔬菜

我有兴趣计算有多少人吃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)用于PP,AA,PA和AP。为什么子数据集(PP,AA,PA和AP)在这种情况下为空?

2 个答案:

答案 0 :(得分:3)

您可以在没有明确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

答案 1 :(得分:1)

您需要更改为PP <- dataframe[dataframe[[fruit]] == '2' & dataframe[[veg]] == '2',]和其他人,水果是字符串,数据框$ fruit不是列