[英]R An if else statement inside a for loop
I have a data table made of 3 columns that is assigned to a variable g.我有一个由 3 列组成的数据表,分配给变量 g。
g
# V1 V2 V3
# [1,] 1 Yes 3
# [2,] 4 No 6
# [3,] 7 No 9
# ...
I'm trying to create a list, m, by checking to see if the values in g[,2] are "Yes" or "No", and then pasting some string into m.我试图通过检查 g[,2] 中的值是“是”还是“否”来创建一个列表 m,然后将一些字符串粘贴到 m 中。
m <- for(i in 1:nrow(g)){
if(g[i,2]=='No'){
paste0("ABC", g[i,1], "DEF", g[i,2], "GHI", g[i,3],"\n")
} else if(g[i,2]=='Yes'){
paste0("GHI", g[i,1], "DEF", g[i,2], "ABC", g[i,3],"\n")
} else {NULL}
}
m
# NULL
However when I try to return m, it just returns NULL.但是,当我尝试返回 m 时,它只返回 NULL。 I want m to look like this:我希望我看起来像这样:
m
# ABC1DEFYesGHI3
# GHI2DEFNoABC9
Can someone point out what I am doing wrong here?有人可以指出我在这里做错了什么吗? Thank you very much!非常感谢!
A for
loop doesn't return anything in R. Typically you would want to update another variable that you would continue using after the for
loop. for
循环在 R 中不返回任何内容。通常,您希望更新另一个变量,以便在for
循环后继续使用。 For example:例如:
m <- "intialize" # initialize, sometimes better as just `list()`
for(i in 1:nrow(g)){
if(g[i,2]=='No'){
# paste into position i of vector m
m[i] <- paste0("ABC", g[i,1], "DEF", g[i,2], "GHI", g[i,3],"\n")
} else if(g[i,2]=='Yes'){
# paste into position i of vector m
m[i] <- paste0("ABC", g[i,1], "DEF", g[i,2], "GHI", g[i,3],"\n")
} else {
NULL
}
}
m
> ...
You can use print() function and capture.output() as following您可以使用 print() 函数和 capture.output() 如下
#generate data, I used 3 lists, which in your case are 3 columns
col1<-c("Yes","No","Maybe","Yes","No")
col2<-1:5
col3<-5:1
data<-cbind(col1,col2,col3)
#check the data table
# col1 col2 col3
#[1,] "Yes" "1" "5"
#[2,] "No" "2" "4"
#[3,] "Maybe" "3" "3"
#[4,] "Yes" "4" "2"
#[5,] "No" "5" "1"
#here is the function for your transformation
m<-function(col1,col2,col3){
for (i in 1:5){
if (col1[i]=="Yes") {res=paste("ABC",col2[i],"DEF",col3[i],sep="")
print(paste(i, res))}
else if(col1[i]=="No") {res=paste("DEF",col2[i],"ABC",col3[i],sep="")
print(paste(i, res))}
}
}
#capture the results
res<-capture.output(m(data[,"col1"],data[,"col2"],data[,"col3"]))
#show results
as.data.frame(res)
# res
#1 [1] "1 ABC1DEF5"
#2 [1] "2 DEF2ABC4"
#3 [1] "4 ABC4DEF2"
#4 [1] "5 DEF5ABC1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.