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> fooname 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 --
Try: oof1 <- list() ?oof1[foo$name] <- foo$num A.K. On Friday, March 7, 2014 10:43 PM, Keith S Weintraub <kw1958 at 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 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.
> oof <- as.list(foo$num) > names(oof) <- foo$name > oofOn Fri, Mar 7, 2014 at 10:41 PM, Keith S Weintraub <kw1958 at 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 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.
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 > 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)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