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 smartpink...@yahoo.com wrote: Try: oof1 - list() oof1[foo$name] - foo$num A.K. On Friday, March 7, 2014 10:43 PM, Keith S Weintraub kw1...@gmail.com 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?
oof - as.list(foo$num) names(oof) - foo$name oof On Fri, Mar 7, 2014 at 10:41 PM, Keith S Weintraub kw1...@gmail.com 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 kw1...@gmail.com 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?
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.
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?
Answered my own question. ?split Never mind! --j On Wed, Aug 10, 2011 at 7:35 PM, Jonathan Greenberg greenb...@ucdavis.edu 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.
[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] 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.
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.
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 j...@illinois.edu 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 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
Yep, my R-gut was right! Thanks Jean and Greg! --j On Thu, Jul 28, 2011 at 1:29 PM, Jean V Adams jvad...@usgs.gov 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 j...@illinois.edu 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.htmlhttp://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
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.
[R] data frame from list of lists with unequal lengths
Hello, I have a dataset with multiple entries in one field separated by / characters. (The true dataset has long names, 20-odd variables, and hundreds of observations.) v1 v2 1 A L 2 A/B M 3 C N 4 D/E/F O 5 A P 6 C L What I would like is to have a dataset that looks like this instead: my.df v1 v2 1 A L 2 A M 3 B M 4 C N 5 D O 6 E O 7 F O 8 A P 9 C L My original thought was to break the string into variables using strsplit(), create new columns in the data frame using cbind(), and then reshape the dataset with the melt() function. v1.new - as.character(my.df$v1) v1.new - strsplit(v1.new, /) v1.new [[1]] [1] A [[2]] [1] A B [[3]] [1] C [[4]] [1] D E F [[5]] [1] A [[6]] [1] C My next thought was to coerce the list into a data frame, but I ran into an error because the list output from strsplit() does not contain equal length vectors. v1.cols - data.frame(v1.new, check.rows=FALSE) Error in data.frame(A, c(A, B), C, c(D, E, F), A, C, : arguments imply differing number of rows: 1, 2, 3 How can I create a data frame from the unequal length vectors that result from strsplit(my.df$v1)? Am I going about this the wrong way? I have also tried to use colsplit{reshape} without success. Thank you for any advice you can offer. I hope the answer to this question is not too obvious. __ 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 from list of lists with unequal lengths
try this: x v1 v2 1 A L 2 A/B M 3 C N 4 D/E/F O 5 A P 6 C L as.data.frame(do.call(rbind, apply(x, 1, function(.row){ + cbind(strsplit(.row[1], '/')[[1]], .row[2]) + })),row.names='') V1 V2 1 A L 2 A M 3 B M 4 C N 5 D O 6 E O 7 F O 8 A P 9 C L On Mon, Jul 20, 2009 at 4:46 PM, Ben Mazzottabenjamin.mazzo...@tufts.edu wrote: Hello, I have a dataset with multiple entries in one field separated by / characters. (The true dataset has long names, 20-odd variables, and hundreds of observations.) v1 v2 1 A L 2 A/B M 3 C N 4 D/E/F O 5 A P 6 C L What I would like is to have a dataset that looks like this instead: my.df v1 v2 1 A L 2 A M 3 B M 4 C N 5 D O 6 E O 7 F O 8 A P 9 C L My original thought was to break the string into variables using strsplit(), create new columns in the data frame using cbind(), and then reshape the dataset with the melt() function. v1.new - as.character(my.df$v1) v1.new - strsplit(v1.new, /) v1.new [[1]] [1] A [[2]] [1] A B [[3]] [1] C [[4]] [1] D E F [[5]] [1] A [[6]] [1] C My next thought was to coerce the list into a data frame, but I ran into an error because the list output from strsplit() does not contain equal length vectors. v1.cols - data.frame(v1.new, check.rows=FALSE) Error in data.frame(A, c(A, B), C, c(D, E, F), A, C, : arguments imply differing number of rows: 1, 2, 3 How can I create a data frame from the unequal length vectors that result from strsplit(my.df$v1)? Am I going about this the wrong way? I have also tried to use colsplit{reshape} without success. Thank you for any advice you can offer. I hope the answer to this question is not too obvious. __ 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ 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 from list of lists with unequal lengths
Try this: r - strsplit(as.character(x$v1), /) cbind(unlist(r), rep(x$v2, sapply(r, length))) On Mon, Jul 20, 2009 at 5:46 PM, Ben Mazzotta benjamin.mazzo...@tufts.eduwrote: Hello, I have a dataset with multiple entries in one field separated by / characters. (The true dataset has long names, 20-odd variables, and hundreds of observations.) v1 v2 1 A L 2 A/B M 3 C N 4 D/E/F O 5 A P 6 C L What I would like is to have a dataset that looks like this instead: my.df v1 v2 1 A L 2 A M 3 B M 4 C N 5 D O 6 E O 7 F O 8 A P 9 C L My original thought was to break the string into variables using strsplit(), create new columns in the data frame using cbind(), and then reshape the dataset with the melt() function. v1.new - as.character(my.df$v1) v1.new - strsplit(v1.new, /) v1.new [[1]] [1] A [[2]] [1] A B [[3]] [1] C [[4]] [1] D E F [[5]] [1] A [[6]] [1] C My next thought was to coerce the list into a data frame, but I ran into an error because the list output from strsplit() does not contain equal length vectors. v1.cols - data.frame(v1.new, check.rows=FALSE) Error in data.frame(A, c(A, B), C, c(D, E, F), A, C, : arguments imply differing number of rows: 1, 2, 3 How can I create a data frame from the unequal length vectors that result from strsplit(my.df$v1)? Am I going about this the wrong way? I have also tried to use colsplit{reshape} without success. Thank you for any advice you can offer. I hope the answer to this question is not too obvious. __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[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.