나는 flummoxed입니다. 두 열의 값에 따라 df의 특정 행을 분리하려고합니다. 언제나처럼 나는 연습 데이터에서 이것을 먼저 시도한다. 내 코드가 잘 작동합니다.대괄호 여러 열 R
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의 요소이며 날짜는 dfs의 둘 다입니다.
내가 대신 내 데이터를 부분 집합 해봤이를 해결하기위한 노력의 일환,하지만 나를 위해 작동하지 않습니다 중 하나 (그러나 실제로 데이터에 대한 작업 않습니다)
x<-subset(lti_first, hai_atc=="V07AY03" | hai_atc=="A11JC94" & (date_of_claim>="2010-04-01" & date_of_claim<"2010-10-01"))
내가 무슨 일을하고 있는가? 나에게 내 코드는 똑같아 보인다! dput에 넣어
샘플 안양
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")
** 실제 데이터가 너무 크고, 여기 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 $ 과일 % % c ('키위', '오렌지', '사과') & (df2 $ dates> = "2010-04-01"& df2 $ dates < "2010-10 -01 "),]. 오류의 재현 가능한 예제를 제공 할 수 없다면 여기서 도움을 줄 것입니다. – mnel
재현 가능한 예를 들어, 예기치 않은 결과를주는 행만 전체 데이터 프레임이 필요하지 않습니다. – Aaron