简体   繁体   English

在 R 中循环通过 DataFrame

[英]Looping through DataFrame in R

Some of you might recognise the example below as I have asked a few questions on it (Thank you for your support - I am still trying to learn R and the syntax!)你们中的一些人可能会认出下面的示例,因为我已经提出了一些问题(感谢您的支持 - 我仍在努力学习 R 和语法!)

What I want to do is loop through the entries of a for loop - I want to check if the average is not equal to the sum of BDA and DAD.我想要做的是遍历 for 循环的条目 - 我想检查平均值是否不等于 BDA 和 DAD 的总和。 I know I can just run the line above the for loop which will do it, but I'm trying to figure out how to access elements of the DataFrame individually.我知道我可以只运行 for 循环上方的行来执行此操作,但我试图弄清楚如何单独访问 DataFrame 的元素。 I am getting an error with the below code (I appreciate there's some redundant code here - I am just playing around with it!)下面的代码出现错误(我很欣赏这里有一些冗余代码 - 我只是在玩弄它!)


DAD <- c(80, 65, 50)
BDA <- c(70, 50, 80.4)

gender <- as.factor(c("F", "M", "M"))
nationality <- as.factor(c("IRL", "UK", "IRL"))
age <- c(20, 21, 22)

age <- as.integer(age)
DAD <- as.integer(DAD)
BDA <- as.integer(BDA)

student <- data.frame(name, age, gender, nationality, DAD, BDA)

#student$average <- as.double(as.double(student$BDA) + as.double(student$DAD))/2
#The above will do what I want if placed before the for loop, but I want to #figure it out 

student <- rbind(student, c("Dennis", 23, "M", "UK", 55, 70))

student <- rbind(student, c("Bennis", 23, "M", "UK", 55, 70, NA)) 

for (row in 1:nrow(student)) {
  avg = (as.double(student[row]$BDA) + as.double(student[row]$DAD))/2
  print(avg)
  if(as.double(student[row]$average) != avg) { 
    student[row]$average = (student[row]$BDA + student[row]$DAD)/2
    }  

I'm not sure why you want to use a for -loop.我不确定您为什么要使用for循环。 But your elements inside your for -loop do not depend on row which is the iteration variable of your loop.但是for -loop 中的元素不依赖于循环的迭代变量row Therefore your loop should look something like this:因此,您的循环应如下所示:

  for (row in 1:nrow(student)) {
    avg[row] = (as.double(student$BDA[row]) + as.double(student$DAD[row]))/2
    print(avg[row])
    if(as.double(student$average[row]) != avg[row]) { 
      student$average[row] = (student$BDA[row] + student$DAD[row])/2
    }  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM