Simon Kiss
2012-Nov-12 22:34 UTC
[R] select different variables from a list of data frames
Hi: How do I select different variables from a list of data frames. I have a list of 13 that looks like below. Each data frame has more variables than I need. How do I go through the list and select the variables that I need. In the example below, I need to get the variables "a", and "q10" and "q14" to be returned to two separate data frames. Thank you. Yours, Simon Kiss #####Sample data mylist<-list(df1=data.frame(a=seq(1,10,1), c=seq(1,109,1), q10=rep('favour', 10)), df2=data.frame(a=seq(1,10,1), b=seq(15,24,1), q14=rep('favour', 10))) #The variables with different names that I need are q<-c('q10', 'q14') #My current code dat<-mapply(function(x,y) { data.frame(a=x$a, y$q) }, x=mylist, y=q)
Hi Arun: Exactly. That's exactly what I'm looking for! On 2012-11-12, at 5:55 PM, arun kirshna [via R] wrote:> Hi, > Seems like there was a typo in "mylist" > mylist<-list(df1=data.frame(a=seq(1,10,1), c=seq(1,10,1), q10=rep('favour', 10)), df2=data.frame(a=seq(1,10,1), b=seq(15,24,1), q14=rep('favour', 10))) > Are you looking for something like this? > lapply(mylist,function(x) x[colnames(x)%in% c("a","q10","q14")]) > #$df1 > # a q10 > #1 1 favour > #2 2 favour > #3 3 favour > #4 4 favour > #5 5 favour > #6 6 favour > #7 7 favour > #8 8 favour > #9 9 favour > #10 10 favour > > #$df2 > # a q14 > #1 1 favour > #2 2 favour > #3 3 favour > #4 4 favour > #5 5 favour > #6 6 favour > #7 7 favour > #8 8 favour > #9 9 favour > #10 10 favour > A.K. > > If you reply to this email, your message will be added to the discussion below: > http://r.789695.n4.nabble.com/select-different-variables-from-a-list-of-data-frames-tp4649342p4649343.html > This email was sent by arun kirshna (via Nabble) > To receive all replies by email, subscribe to this discussion********************************* Simon J. Kiss, PhD Assistant Professor, Wilfrid Laurier University 73 George Street Brantford, Ontario, Canada N3T 2C9 Cell: +1 905 746 7606 Please avoid sending me Word, PowerPoint or Excel attachments. Sending these documents puts pressure on many people to use Microsoft software and helps to deny them any other choice. In effect, you become a buttress of the Microsoft monopoly. To convert to plain text choose Text Only or Text Document as the Save As Type. Your computer may also have a program to convert to PDF format. Select File, then Print. Scroll through available printers and select the PDF converter. Click on the Print button and enter a name for the PDF file when requested. -- View this message in context: http://r.789695.n4.nabble.com/select-different-variables-from-a-list-of-data-frames-tp4649342p4649344.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Rui Barradas
2012-Nov-12 23:29 UTC
[R] select different variables from a list of data frames
Hello, Try the following. lapply(mylist, function(x) x[,grep("a|q", names(x))]) Hope this helps, Rui Barradas Em 12-11-2012 22:34, Simon Kiss escreveu:> Hi: > How do I select different variables from a list of data frames. > I have a list of 13 that looks like below. Each data frame has more variables than I need. How do I go through the list and select the variables that I need. > In the example below, I need to get the variables "a", and "q10" and "q14" to be returned to two separate data frames. > Thank you. > Yours, Simon Kiss > > #####Sample data > mylist<-list(df1=data.frame(a=seq(1,10,1), c=seq(1,109,1), q10=rep('favour', 10)), df2=data.frame(a=seq(1,10,1), b=seq(15,24,1), q14=rep('favour', 10))) > > #The variables with different names that I need are > q<-c('q10', 'q14') > #My current code > > dat<-mapply(function(x,y) { > data.frame(a=x$a, y$q) > }, x=mylist, y=q) > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi, In addition to: mylist<-list(df1=data.frame(a=seq(1,10,1), c=seq(1,10,1), q10=rep('favour', 10)), df2=data.frame(a=seq(1,10,1), b=seq(15,24,1), q14=rep('favour', 10))) lapply(mylist,function(x) x[colnames(x)%in% c("a","q10","q14")]) you could also use:?lapply(seq_along(mylist),function(i) mylist[[i]][names(mylist[[i]])%in% c("a","q10","q14")]) #[[1]] #??? a??? q10 #1?? 1 favour #2?? 2 favour #3?? 3 favour #4?? 4 favour #5?? 5 favour #6?? 6 favour #7?? 7 favour #8?? 8 favour #9?? 9 favour #10 10 favour #[[2]] ?# ? a??? q14 #1?? 1 favour #2?? 2 favour #3?? 3 favour #4?? 4 favour #5?? 5 favour #6?? 6 favour #7?? 7 favour #8?? 8 favour #9?? 9 favour #10 10 favour A.K. ----- Original Message ----- From: Simon Kiss <sjkiss at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, November 12, 2012 5:34 PM Subject: [R] select different variables from a list of data frames Hi: How do I select different variables from a list of data frames. I have a list of 13 that looks like below.? Each data frame has more variables than I need.? How do I go through the list and select the variables that I need. In the example below, I need to get the variables "a", and "q10" and "q14" to be returned to two separate data frames. Thank you. Yours, Simon Kiss #####Sample data ? mylist<-list(df1=data.frame(a=seq(1,10,1), c=seq(1,109,1), q10=rep('favour', 10)), df2=data.frame(a=seq(1,10,1), b=seq(15,24,1), q14=rep('favour', 10))) #The variables with different names that I need are q<-c('q10', 'q14') #My current code dat<-mapply(function(x,y) { ? data.frame(a=x$a, y$q) }, x=mylist, y=q) ______________________________________________ R-help at r-project.org mailing list 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.