Dear all,
would appreciate your suggestions on subsetting a dataframe : please let's
consider an example dataframe df:
dd<-c(1,2,3)
rows<-c("A1","A2","A3")
columns<-c("B1","B2","B3")
numbers <- c(400, 500, 600)
df <- dataframe(dd,rows,columns, numbers)
and a vector : test_rows <-c("A1","A3") ;
how could I subset the dataframe df function of vector test_rows, in such a
way that only the lines of dataframe df (df$rows) that match the elements
of test_rows ("A1" and "A3") are listed ?
thank you very much,
-- bogdan
[[alternative HTML version deleted]]
Lots of ways to do this, I use %in% with bracket notation [row, column]. The empty column argument below returns all columns but you could have conditional logic there as well. dd[dd$rows %in% test_rows, ] On Mon, Jun 8, 2015 at 6:44 PM, Bogdan Tanasa <tanasa at gmail.com> wrote:> Dear all, > > would appreciate your suggestions on subsetting a dataframe : please let's > consider an example dataframe df: > > dd<-c(1,2,3) > rows<-c("A1","A2","A3") > columns<-c("B1","B2","B3") > numbers <- c(400, 500, 600) > df <- dataframe(dd,rows,columns, numbers) > > and a vector : test_rows <-c("A1","A3") ; > > how could I subset the dataframe df function of vector test_rows, in such a > way that only the lines of dataframe df (df$rows) that match the elements > of test_rows ("A1" and "A3") are listed ? > > thank you very much, > > -- bogdan > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Use is.element(elements,set), or its equivalent, elements %in% set:
df <- data.frame(dd = c(1, 2, 3),
rows = c("A1", "A2", "A3"),
columns = c("B1", "B2", "B3"),
numbers = c(400, 500, 600))
test_rows <-c("A1","A3")
df[ is.element(df$rows, test_rows), ]
# dd rows columns numbers
#1 1 A1 B1 400
#3 3 A3 B3 600
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Mon, Jun 8, 2015 at 3:44 PM, Bogdan Tanasa <tanasa at gmail.com> wrote:
> Dear all,
>
> would appreciate your suggestions on subsetting a dataframe : please
let's
> consider an example dataframe df:
>
> dd<-c(1,2,3)
> rows<-c("A1","A2","A3")
> columns<-c("B1","B2","B3")
> numbers <- c(400, 500, 600)
> df <- dataframe(dd,rows,columns, numbers)
>
> and a vector : test_rows <-c("A1","A3") ;
>
> how could I subset the dataframe df function of vector test_rows, in such a
> way that only the lines of dataframe df (df$rows) that match the elements
> of test_rows ("A1" and "A3") are listed ?
>
> thank you very much,
>
> -- bogdan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]