R multiple conditions in if statement [duplicate]

给你一囗甜甜゛ 提交于 2019-12-29 06:45:39

问题


I have read many of the if statement posts but have not been able to find an answer to my simple problem. I would like to create a new column in the data frame 'tester' based on a multiple condition if statement.

tester<- as.data.frame(matrix(data=c(seq(1,300,by=1.5)), ncol=4))

if (tester$V3> 200 && tester$V4>250){tester[,5] <- "one"} else tester$V5 <-NA

This gives me NAs for the entire column even though the last 17 rows are TRUE for both cases and should be "one". What is happening here? Thank you for your help!


回答1:


Read this thread R - boolean operators && and ||.

Basically, the & is vectorized, i.e. it acts on each element of the comparison returning a logical array with the same dimension as the input. && is not, returning a single logical.



来源:https://stackoverflow.com/questions/39582031/r-multiple-conditions-in-if-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!