【问题标题】:square brackets multiple columns R方括号多列 R
【发布时间】:2013-08-01 01:15:47
【问题描述】:

我很困惑。我正在尝试根据两列中的值来隔离 df 的某些行。和往常一样,我首先在实践数据中尝试这个。我的代码工作正常。

data1<-df2[df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple"  & (df2$dates>= "2010-04-01" & df2$dates<  "2010-10-01"), ]

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

 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"), ]

我的练习数据和真实数据中的变量结构完全相同相同。 Fruits/hai_atc 是两个 dfs 中的因子,日期是 as.Dates 在两个 dfs 中。

为了解决这个问题,我尝试对我的数据进行子集化,但这对我也不起作用(但对练习数据有效)

x<-subset(lti_first, hai_atc=="V07AY03" | hai_atc=="A11JC94" & (date_of_claim>="2010-04-01" & date_of_claim<"2010-10-01"))

我做错了什么?对我来说,我的代码看起来一模一样!

样本df

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 代替

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 ...

【问题讨论】:

  • 您可以通过使用%in% 而不是== 来避免许多对| 的调用。例如..df2[df2$fruit %in% c('kiwi','orange','apple') &amp; (df2$dates&gt;= "2010-04-01" &amp; df2$dates&lt; "2010-10-01"), ]。除非你能提供一个可重现的例子(你的错误),否则这里很难提供帮助。
  • 举个可重现的例子,我们不需要你的整个数据框,只需要给你意外结果的那一行。

标签: r subset


【解决方案1】:

我认为扩展@Aaron 的评论很重要。您遇到的问题是由于使用%in% 避免的所有OR 语句周围缺少括号,而不是OR 语句在提取函数[ 中不起作用。您的玩具示例实际上并没有完全按照您的要求工作 - 有一个 orange 水果,日期为 2010-12-01。没有出现其他问题只是偶然。

这段代码中布尔逻辑的读取方式

df2[df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple"  & (df2$dates>= "2010-04-01" & df2$dates<  "2010-10-01"), ]

是:

我想要水果是猕猴桃的所有 df2 行,水果是水果的所有行 橙色,所有水果为苹果且日期为 2010 年 3 月 31 日至 2010 年 10 月 1 日之间。

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

现在添加一对括号:

df2[(df2$fruit=="kiwi" |  df2$fruit=="orange" | df2$fruit=="apple")  & (df2$dates >= "2010-04-01" & df2$dates <  "2010-10-01"), ]

这段代码说:

我想要水果是猕猴桃、橙子或苹果的所有 df2 行,并且 日期介于 2010 年 3 月 31 日至 2010 年 10 月 1 日之间。

话虽如此,%in% 绝对是正确的选择。

【讨论】:

  • Aosmith - 谢谢你。一个很好的教学点。我现在更清楚自己做错了什么。
【解决方案2】:

这行得通吗?

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的使用。

【讨论】:

  • 另请注意,这与您所拥有的布尔逻辑不同;在您的情况下,等效的布尔逻辑将在三个水果语句周围有括号。
  • 有趣的是,as.Date 似乎不是问题(尽管这似乎是个好主意);这就是我的想法,但是当我测试它时,似乎 R 必须悄悄地进行适当的转换。
  • @Hong Ooi 和@mnel;您的两个解决方案都有效。太感谢了。 Aaron - 我本身并没有收到错误,只是我的代码的日期部分没有被考虑在内。所以没有错误信息。所以这里要吸取的教训是|使用方括号时不起作用?
猜你喜欢
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多