在if语句中的R多个条件 [英] R multiple conditions in if statement

查看:2823
本文介绍了在if语句中的R多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过许多if语句帖子,但未能找到我的简单问题的答案。
我想根据多条件if语句在数据框'tester'中创建一个新列。

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

这给了我整个列的NA,即使最后17行对于两种情况都是TRUE并且应该是1。这里发生了什么?感谢您的帮助!

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!

推荐答案

阅读此主题 R - 布尔运算符&&和||

基本上,& 是矢量化的,即它对每个元素起作用比较返回一个逻辑数组与输入相同的维度。 && 不是,返回一个逻辑。

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.

这篇关于在if语句中的R多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆