【问题标题】:Dataframes in nested for loop. <0 rows> (or 0-length row.names) error嵌套 for 循环中的数据框。 <0 行>(或 0 长度行名称)错误
【发布时间】:2016-11-17 10:40:16
【问题描述】:

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

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 循环中,我收到以下消息:&lt;0 rows&gt; (or 0-length row.names) 用于 PP、AA、PA 和 AP。为什么在这种情况下子数据集(PP、AA、PA 和 AP)是空的?

【问题讨论】:

  • 你需要做PP &lt;- dataframe[dataframe[[fruit]] == '2' &amp; dataframe[[veg]] == '2',]等,dataframe$fruit不是列
  • 在您对FRUITVEG 的计算中,有一个关闭的) 过多

标签: r dataframe subset two-columns


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你需要更改为PP &lt;- dataframe[dataframe[[fruit]] == '2' &amp; dataframe[[veg]] == '2',] 和其他人,fruit 是一个字符串,dataframe$fruit 不是一个列

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2023-01-11
      • 2016-02-07
      • 1970-01-01
      • 2013-06-10
      相关资源
      最近更新 更多