Re: [R] Data Frame to list?
On 3/7/2014 7:41 PM, Keith S Weintraub wrote: Folks, I have a data frame as follows: foo<-structure(list(name = c("A", "B", "C"), num = c(3L, 2L, 1L)), .Names = c("name", "num"), row.names = c(NA, -3L), class = "data.frame") str(foo) 'data.frame': 3 obs. of 2 variables: $ name: chr "A" "B" "C" $ num : int 3 2 1 foo name num 1A 3 2B 2 3C 1 I want to convert this to a list like so: oof<-list(A = 3, B = 2, C = 1) You can do it as a one-liner as well: oof <- setNames(as.list(foo$num), foo$name) str(oof) List of 3 $ A: num 3 $ B: num 2 $ C: num 1 oof $A [1] 3 $B [1] 2 $C [1] 1 Any Suggestions? Thanks, KW -- -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University __ R-help@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.
Re: [R] Data Frame to list?
Arun et al. Thanks, This is exactly what I need. All the best, KW -- On Mar 7, 2014, at 10:59 PM, arun wrote: > Try: > oof1 <- list() > oof1[foo$name] <- foo$num > A.K. > > > > > On Friday, March 7, 2014 10:43 PM, Keith S Weintraub wrote: > Folks, > > I have a data frame as follows: > >> foo<-structure(list(name = c("A", "B", "C"), num = c(3L, 2L, 1L)), .Names = >> c("name", > "num"), row.names = c(NA, -3L), class = "data.frame") > >> str(foo) > 'data.frame': 3 obs. of 2 variables: > $ name: chr "A" "B" "C" > $ num : int 3 2 1 > >> foo > name num > 1A 3 > 2B 2 > 3C 1 > > I want to convert this to a list like so: > >> oof<-list(A = 3, B = 2, C = 1) > >> str(oof) > List of 3 > $ A: num 3 > $ B: num 2 > $ C: num 1 > >> oof > $A > [1] 3 > > $B > [1] 2 > > $C > [1] 1 > > Any Suggestions? > > Thanks, > KW > > -- > > __ > R-help@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. > __ R-help@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.
Re: [R] Data Frame to list?
Try: oof1 <- list() oof1[foo$name] <- foo$num A.K. On Friday, March 7, 2014 10:43 PM, Keith S Weintraub wrote: Folks, I have a data frame as follows: > foo<-structure(list(name = c("A", "B", "C"), num = c(3L, 2L, 1L)), .Names = > c("name", "num"), row.names = c(NA, -3L), class = "data.frame") > str(foo) 'data.frame': 3 obs. of 2 variables: $ name: chr "A" "B" "C" $ num : int 3 2 1 > foo name num 1 A 3 2 B 2 3 C 1 I want to convert this to a list like so: > oof<-list(A = 3, B = 2, C = 1) > str(oof) List of 3 $ A: num 3 $ B: num 2 $ C: num 1 > oof $A [1] 3 $B [1] 2 $C [1] 1 Any Suggestions? Thanks, KW -- __ R-help@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. __ R-help@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.
Re: [R] Data Frame to list?
> oof <- as.list(foo$num) > names(oof) <- foo$name > oof On Fri, Mar 7, 2014 at 10:41 PM, Keith S Weintraub wrote: > Folks, > > I have a data frame as follows: > >> foo<-structure(list(name = c("A", "B", "C"), num = c(3L, 2L, 1L)), .Names = >> c("name", > "num"), row.names = c(NA, -3L), class = "data.frame") > >> str(foo) > 'data.frame': 3 obs. of 2 variables: > $ name: chr "A" "B" "C" > $ num : int 3 2 1 > >> foo > name num > 1A 3 > 2B 2 > 3C 1 > > I want to convert this to a list like so: > >> oof<-list(A = 3, B = 2, C = 1) > >> str(oof) > List of 3 > $ A: num 3 > $ B: num 2 > $ C: num 1 > >> oof > $A > [1] 3 > > $B > [1] 2 > > $C > [1] 1 > > Any Suggestions? > > Thanks, > KW > > -- > > __ > R-help@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. __ R-help@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.
[R] Data Frame to list?
Folks, I have a data frame as follows: > foo<-structure(list(name = c("A", "B", "C"), num = c(3L, 2L, 1L)), .Names = > c("name", "num"), row.names = c(NA, -3L), class = "data.frame") > str(foo) 'data.frame': 3 obs. of 2 variables: $ name: chr "A" "B" "C" $ num : int 3 2 1 > foo name num 1A 3 2B 2 3C 1 I want to convert this to a list like so: > oof<-list(A = 3, B = 2, C = 1) > str(oof) List of 3 $ A: num 3 $ B: num 2 $ C: num 1 > oof $A [1] 3 $B [1] 2 $C [1] 1 Any Suggestions? Thanks, KW -- __ R-help@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.
Re: [R] Data frame to list?
On Aug 11, 2011, at 8:24 AM, Michael Karol wrote: Perhaps the split() function would do. Something like split(dfrm, 1:nrow(dfrm)) or split(dfrm, rownames(dfrm)) -- Regards, Michael -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org ] On Behalf Of Jonathan Greenberg Sent: Wednesday, August 10, 2011 10:36 PM To: r-help Subject: [R] Data frame to list? R-helpers: Is there an easy way to transfer a data frame to a list, where each list element is a dataframe containing a single row from the original data frame? --j David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Data frame to list?
Perhaps the split() function would do. Regards, Michael -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Greenberg Sent: Wednesday, August 10, 2011 10:36 PM To: r-help Subject: [R] Data frame to list? R-helpers: Is there an easy way to transfer a data frame to a list, where each list element is a dataframe containing a single row from the original data frame? --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@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. __ R-help@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.
[R] Data frame to list?
R-helpers: Is there an easy way to transfer a data frame to a list, where each list element is a dataframe containing a single row from the original data frame? --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@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.
Re: [R] Data frame to list?
Answered my own question. ?split Never mind! --j On Wed, Aug 10, 2011 at 7:35 PM, Jonathan Greenberg wrote: > R-helpers: > > Is there an easy way to transfer a data frame to a list, where each > list element is a dataframe containing a single row from the original > data frame? > > --j > > -- > Jonathan A. Greenberg, PhD > Assistant Project Scientist > Center for Spatial Technologies and Remote Sensing (CSTARS) > Department of Land, Air and Water Resources > University of California, Davis > One Shields Avenue > Davis, CA 95616 > Phone: 415-763-5476 > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 > -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@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.
Re: [R] Data frame to list
On Jul 28, 2011, at 4:14 PM, Jonathan Greenberg wrote: I'm hoping this is an easy problem that I'm missing something obvious. Given: x=c(1,1,1,2,2,3,3,3) y=c(1:length(x)) dataframe=data.frame(x,y) I would like to convert this to a list for use with certain functions, where each entry of the list is a subsetted dataframe based on dataframe$x ?split > split(dataframe, x) $`1` x y 1 1 1 2 1 2 3 1 3 $`2` x y 4 2 4 5 2 5 $`3` x y 6 3 6 7 3 7 8 3 8 I can do this "brute force" by a for-next loop: unique_x=unique(dataframe$x) unique_x_N=length(unique_x) dataframe_to_list=vector(mode="list",length=unique_x_N) for(i in 1:unique_x_N) { dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) } My R-gut is telling me there's a much more efficient way of doing this -- is it right? --j __ R-help@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. David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Data frame to list
Yep, my R-gut was right! Thanks Jean and Greg! --j On Thu, Jul 28, 2011 at 1:29 PM, Jean V Adams wrote: > > Try this: > > split(dataframe, dataframe$x) > > Jean > > > `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> > > Jean V. Adams > Statistician > U.S. Geological Survey > Great Lakes Science Center > 223 East Steinfest Road > Antigo, WI 54409 USA > > > From: Jonathan Greenberg To: r-help < > r-help@r-project.org> Date: 07/28/2011 03:22 PM Subject: > [R] Data frame to list > Sent by: r-help-boun...@r-project.org > -- > > > > I'm hoping this is an easy problem that I'm missing something obvious. > Given: > > x=c(1,1,1,2,2,3,3,3) > y=c(1:length(x)) > dataframe=data.frame(x,y) > > I would like to convert this to a list for use with certain functions, > where each entry of the list is a subsetted dataframe based on > dataframe$x > > I can do this "brute force" by a for-next loop: > > unique_x=unique(dataframe$x) > unique_x_N=length(unique_x) > dataframe_to_list=vector(mode="list",length=unique_x_N) > for(i in 1:unique_x_N) > { > dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) > > } > > My R-gut is telling me there's a much more efficient way of doing this > -- is it right? > > --j > > __ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Data frame to list
Try this: split(dataframe, dataframe$x) Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA From: Jonathan Greenberg To: r-help Date: 07/28/2011 03:22 PM Subject: [R] Data frame to list Sent by: r-help-boun...@r-project.org I'm hoping this is an easy problem that I'm missing something obvious. Given: x=c(1,1,1,2,2,3,3,3) y=c(1:length(x)) dataframe=data.frame(x,y) I would like to convert this to a list for use with certain functions, where each entry of the list is a subsetted dataframe based on dataframe$x I can do this "brute force" by a for-next loop: unique_x=unique(dataframe$x) unique_x_N=length(unique_x) dataframe_to_list=vector(mode="list",length=unique_x_N) for(i in 1:unique_x_N) { dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) } My R-gut is telling me there's a much more efficient way of doing this -- is it right? --j __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Data frame to list
?split -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Jonathan Greenberg > Sent: Thursday, July 28, 2011 2:14 PM > To: r-help > Subject: [R] Data frame to list > > I'm hoping this is an easy problem that I'm missing something obvious. > Given: > > x=c(1,1,1,2,2,3,3,3) > y=c(1:length(x)) > dataframe=data.frame(x,y) > > I would like to convert this to a list for use with certain functions, > where each entry of the list is a subsetted dataframe based on > dataframe$x > > I can do this "brute force" by a for-next loop: > > unique_x=unique(dataframe$x) > unique_x_N=length(unique_x) > dataframe_to_list=vector(mode="list",length=unique_x_N) > for(i in 1:unique_x_N) > { > dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) > > } > > My R-gut is telling me there's a much more efficient way of doing this > -- is it right? > > --j > > __ > R-help@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. __ R-help@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.
[R] Data frame to list
I'm hoping this is an easy problem that I'm missing something obvious. Given: x=c(1,1,1,2,2,3,3,3) y=c(1:length(x)) dataframe=data.frame(x,y) I would like to convert this to a list for use with certain functions, where each entry of the list is a subsetted dataframe based on dataframe$x I can do this "brute force" by a for-next loop: unique_x=unique(dataframe$x) unique_x_N=length(unique_x) dataframe_to_list=vector(mode="list",length=unique_x_N) for(i in 1:unique_x_N) { dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) } My R-gut is telling me there's a much more efficient way of doing this -- is it right? --j __ R-help@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.