首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方括号多列R

方括号多列R
EN

Stack Overflow用户
提问于 2013-08-01 01:15:47
回答 2查看 3.3K关注 0票数 2

我有点迷糊了。我试图根据两列中的值来隔离特定的df行。和往常一样,我首先在实际数据中尝试这一点。我的代码很好用。

代码语言:javascript
复制
data1<-df2[df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple"  & (df2$dates>= "2010-04-01" & df2$dates<  "2010-10-01"), ]

当我在我的真实数据上尝试相同的代码时,它不起作用。它收集我需要的“水果”,但忽略了我的日期范围请求。

代码语言:javascript
复制
 data1<-lti_first[lti_first$hai_atc=="C10AA01" | lti_first$hai_atc=="C10AA03" | lti_first$hai_atc=="C10AA04" | lti_first$hai_atc=="C10AA05" | lti_first$hai_atc=="C10AA07" | lti_first$hai_atc=="C10AB02" |lti_first$hai_atc=="C10AA04" |lti_first$hai_atc=="C10AB08" | lti_first$hai_atc=="C10AX09" & (lti_first$date_of_claim >= "2010-04-01" & lti_first$date_of_claim<"2010-10-01"), ]

我的实践数据和实际数据中的变量的结构是完全相同的。果实/hai_atc是dfs的影响因子,dfs中的枣是as.Dates。

为了解决这一问题,我尝试将我的数据替换为子数据,但这对我也不起作用(但在实践数据上确实有效)。

代码语言:javascript
复制
x<-subset(lti_first, hai_atc=="V07AY03" | hai_atc=="A11JC94" & (date_of_claim>="2010-04-01" & date_of_claim<"2010-10-01"))

我做错了什么?在我看来,我的代码看起来是一样的!

样本df

代码语言:javascript
复制
names<-c("tom", "mary", "tom", "john", "mary",
 "tom", "john", "mary", "john", "mary", "tom", "mary", "john", "john")
dates<-as.Date(c("2010-02-01", "2010-05-01", "2010-03-01", 
"2010-07-01", "2010-07-01", "2010-06-01", "2010-09-01",
 "2010-07-01", "2010-11-01", "2010-09-01", "2010-08-01", 
"2010-11-01", "2010-12-01", "2011-01-01"))
fruit<-as.character(c("apple", "orange", "banana", "kiwi",
 "apple", "apple", "apple", "orange", "banana", "apple",
 "kiwi", "apple", "orange", "apple"))
age<-as.numeric(c(60,55,60,57,55,60,57,55,57,55,60,55, 57,57))
sex<-as.character(c("m","f","m","m","f","m","m",
 "f","m","f","m","f","m", "m"))
df2<-data.frame(names,dates, age, sex, fruit)
df2


dput(df2)
structure(list(names = structure(c(3L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 1L, 2L, 3L, 2L, 1L, 1L), .Label = c("john", "mary", "tom"
), class = "factor"), dates = structure(c(14641, 14730, 14669, 
14791, 14791, 14761, 14853, 14791, 14914, 14853, 14822, 14914, 
14944, 14975), class = "Date"), age = c(60, 55, 60, 57, 55, 60, 
57, 55, 57, 55, 60, 55, 57, 57), sex = structure(c(2L, 1L, 2L, 
2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L), .Label = c("f", 
"m"), class = "factor"), fruit = structure(c(1L, 4L, 2L, 3L, 
1L, 1L, 1L, 4L, 2L, 1L, 3L, 1L, 4L, 1L), .Label = c("apple", 
"banana", "kiwi", "orange"), class = "factor")), .Names = c("names", 
"dates", "age", "sex", "fruit"), row.names = c(NA, -14L), class = "data.frame")

**实际数据太大,无法输入dput,这里有一个str

代码语言:javascript
复制
str(sample_lti_first)
'data.frame':   20 obs. of  5 variables:
 $ hai_dispense_number: Factor w/ 53485 levels "Patient HAI0000017",..: 22260 22260 2527 24311 24311 24311 24311 13674 13674 13674 ...
 $ sex                : Factor w/ 4 levels "F","M","U","X": 2 2 2 1 1 1 1 1 1 1 ...
 $ hai_age            : int  18 18 27 40 40 40 40 28 28 28 ...
 $ date_of_claim      : Date, format: "2009-10-09" "2009-10-09" "2009-10-18" ...
 $ hai_atc            : Factor w/ 1038 levels "","A01AA01","A01AB03",..: 144 76 859 80 1009 1009 859 81 1008 859 ...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-01 01:21:22

这行得通吗?

代码语言:javascript
复制
data1 <- subset(lti_first,
  (hai_atc %in% c("C10AA01", "C10AA03", "C10AA04", "C10AA05", "C10AA07",
                  "C10AB02", "C10AA04", "C10AB08", "C10AX09")) & 
  (date_of_claim >= as.Date("2010-04-01") & date_of_claim < as.Date("2010-10-01")))

注意%in%as.Date的使用。

票数 2
EN

Stack Overflow用户

发布于 2013-08-01 17:22:31

我认为重要的是对@Aaron的评论进行详细阐述。您遇到的问题是由于使用%in%避免的所有OR语句缺少括号所致,而不是OR语句在提取函数[中不工作。你的玩具例子实际上并没有达到你想要的效果--有一个orange水果和日期2010-12-01。其他的问题并没有出现只是偶然。

读取此代码中的布尔逻辑的方法

代码语言:javascript
复制
df2[df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple"  & (df2$dates>= "2010-04-01" & df2$dates<  "2010-10-01"), ]

是:

我要所有的df2行,水果是猕猴桃,所有行的水果是橙色的,所有行的水果是苹果,日期在3/31/2010和10/1/2010之间。

这就是你得到的-只有苹果被截断到适当的日期范围。实际上,玩具数据集中没有超出日期范围的kiwis。

现在添加一对括号:

代码语言:javascript
复制
df2[(df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple")  & (df2$dates >= "2010-04-01" & df2$dates <  "2010-10-01"), ]

这条守则说:

我想要所有的df2,其中的水果要么是猕猴桃,橙色,或苹果,日期是3/31/2010和10/1/2010。

也就是说,%in%绝对是一条路。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17983708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档