R - brevity when subsetting? -
i'm still new r , of subsetting via pattern:
data[ command produces logical same length data ]
or
subset( data , command produces logical same length data )
for example:
test = c("a", "b","c") ignore = c("b") result = test[ !( test %in% ignore ) ] result = subset( test , !( test %in% ignore ) )
but vaguely remember readings there's shorter/(more readable?) way this? perhaps using "with" function?
can list alternative example above me understand options in subsetting?
i don't know of more succinct way of subsetting specific example, using vectors. may thinking of, regarding with
, subsetting data frames based on conditions using columns data frame. example:
dat <- data.frame(variable1 = runif(10), variable2 = letters[1:10])
if want grab subset of dat
based on condition using variable1
this:
dat[dat$variable1 < 0,]
or can save ourselves having write dat$*
each time using with
:
with(dat,dat[variable1 < 0,])
now, you'll notice didn't save keystrokes doing in case. if have data frame long name, , complicated condition can save bit. see related ?within
command if you're altering data frame in question.
alternatively, can use subset
can same thing:
subset(dat, variable1 < 0)
subset
can handle conditions on columns via select argument.
Comments
Post a Comment