dataframe - combine 2 or more in operator in R -
i want select rows data frame according conditions. select values using % in % operator. used many %in% selecting values.
val1 <- portdata [portdata$pmkval %in% c(na),] val2 <- val1 [val1$quantity %in% c(na),] weigtagedata <- val2 [val2$mktval %in% c(na),] can write these statements in 1 line , select data frame portdata instead of writing inefficient code?
first since you're checking na, can use nice handy function is.na(.). is,
val1 <- portdata [is.na(portdata$pmkval), ] val2 <- val1[is.na(val1$quantity), ] weigtagedata <- val2[is.na(val2$mktval), ] now, can use & connect these in single command follows:
weigtagedate <- portdata[is.na(portdata$pmkval) & is.na(portdata$quantity) & is.na(portdata$mktval), ] even nicer use with here, dont have use portdata$ every time.
weigtagedata <- portdata[with(portdata, is.na(pmkval) & is.na(quantity) & is.na(mktval)), ] of course same translates %in% well. it's not necessary here.
Comments
Post a Comment