- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很困惑。我试图根据两列中的值隔离 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 是两个 df 中的因子,日期是两个 df 中的 as.Dates。
为了解决这个问题,我尝试过对数据进行子集化,但这对我也不起作用(但对练习数据有效)
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 ...
最佳答案
我认为扩展@Aaron 的评论很重要。您遇到的问题是由于使用 %in%
避免的所有 OR 语句周围缺少括号引起的,而不是 OR 语句在提取函数 [
。你的玩具示例实际上并没有完全按照你想要的方式工作 - 有一个日期为 2010-12-01
的 orange
水果。没有出现其他问题纯属偶然。
这段代码中 bool 逻辑的读取方式
df2[df2$fruit=="kiwi" | df2$fruit=="orange" | df2$fruit=="apple" & (df2$dates>= "2010-04-01" & df2$dates< "2010-10-01"), ]
是:
I want all rows of df2 where fruit is kiwi, all rows where fruit is orange, and all rows where fruit is apple and the dates are between 3/31/2010 and 10/1/2010.
这就是您得到的 - 只有苹果被截断到适当的日期范围。 toy 数据集中实际上没有超出日期范围的猕猴桃。
现在添加一对括号:
df2[(df2$fruit=="kiwi" | df2$fruit=="orange" | df2$fruit=="apple") & (df2$dates >= "2010-04-01" & df2$dates < "2010-10-01"), ]
这段代码说:
I want all the rows of df2 where fruit is either kiwi, orange, or apple and the dates are between 3/31/2010 and 10/1/2010.
综上所述,%in%
绝对是正确的选择。
关于r - 方括号多列 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983708/
我一直想知道两者之间有什么区别 mov esi,eax 和 mov [esi],eax 曾经是。 感谢任何帮助。 最佳答案 mov esi,eax 将寄存器eax的内容写入寄存器esi。 mov [e
如果有人能帮助我满足 JAVA 正则表达式要求,我将不胜感激我有一个像 "/ABC/KLM[XYZ/ABC/KLM]/ABC"这样的字符串 我想替换所有不用方括号括起来的ABC。在这种情况下,只应找到
我需要替换 key:value 对周围的方括号,类似于以下内容。任何帮助深表感谢! “属性”中的数据如下所示: name: property1 value: [12345667:97764458] *
我正在尝试使用某种 grok 模式来使用以下日志记录格式: *Sun 07:05:18.372 INFO [main] [userID] perf - 0ms - select x from y 我
我在 R 中有一个函数,看起来有点像这样: setMethod('[', signature(x="stack"),definition=function(x,i,j,drop){ new('cl
我将这些定义放在一个文件中: x = 'a' : 'b' : 'c' : [] y = ['a', 'b', 'c'] (重要的是在文件中定义它们,而不是在 GHCi 中,因为在后一种情况下,事情变得
我喜欢将DocBlockr插件用于精美文字,但我希望自己的评论有所不同。 正常出现: 以及我希望它们出现的方式: 因此,是否有这样做的type,description和[]方括号呢?我已经搜寻了一下,
让我们考虑以下是我的对象: var n = {"aa":"x","dd":'d'}; 我在 Object.assign 中使用方括号.它给出了以下结果。 [aa: "x", dd: "d"] .最终代
我正在尝试使用正则表达式从 KEY PAIR VALUE 中找出 VALUE。 VALUE 可以有 [ ](方括号)。如果 VALUE 中存在 [ ](方括号),那么我只想提取 [ ] 之外的字符。
这个问题在这里已经有了答案: What is array literal notation in javascript and when should you use it? (4 个答案) 关闭
这个问题在这里已经有了答案: What do square brackets around a property name in an object literal mean? (2 个答案) 关闭
我有一个语法标记指定为: list_value = Suppress(oneOf("[ (")) + Group( delimitedList(string_value | int_value
如何替换这种格式的标记: [a href="/my_page" style="font-size: 13px"]click me[/a] 到 click me 使用 preg_replace()? 我
使用下面的代码,我可以将 Number123(45) 转换为 Number。 $string = 'Number123(45)'; $string2 = preg_replace('/[0-9]+
我知道硬括号('[' 和 ']')用于标识数组,但是在搜索如何使用事件时,我偶然发现了它们的另一种用法,并且想知道它到底意味着什么...... 我看到的代码 ( link ) 如下所示: // evh
This question already has an answer here: gRPC/Protobuf 3 syntax: what is the difference between rpc
在vim中,您可以通过vi“,vi [,vi(... 例如,如果您有这样的一行: x = "difference between vim and emacs" 并且光标位于这些引号之间的任意位置,然后
AngularJS将方括号用作其指令的参数 (input[number]) 但是Jade also uses square brackets for class attributes。 所以这行不通
正则表达式中的点. 匹配任何单个字符。为了使正则表达式匹配点,必须对点进行转义:\. It has been pointed out to me方括号 [] 内的点不必转义。例如,表达式:[.]{3}
这个问题已经有答案了: What is the difference between square brackets and parentheses in a regex? (3 个回答) 已关闭 8
我是一名优秀的程序员,十分优秀!