I have a set of character vectors of uneven length
that I have stored in a list. I can easily enough get
any column of them using lapply but what I want is to
be able to create a matrix of them. Other than some
kind of brute force looping approach I have drawn a
blank.
Would somebody please suggest something? Thanks
Example.
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
Looking for the perfect gift? Give the gift of Flickr!
Splitting a set of vectors in a list
15 messages · Erik Iverson, Henrique Dallazuanna, Benilton Carvalho +3 more
What would you want your output matrix to look like given mylist?
John Kane wrote:
I have a set of character vectors of uneven length
that I have stored in a list. I can easily enough get
any column of them using lapply but what I want is to
be able to create a matrix of them. Other than some
kind of brute force looping approach I have drawn a
blank.
Would somebody please suggest something? Thanks
Example.
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
Looking for the perfect gift? Give the gift of Flickr!
______________________________________________ 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.
If I understand correctly, try this: as.data.frame(lapply(mylist, `[`, 1:max(unlist(lapply(mylist, length)))))
On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
I have a set of character vectors of uneven length
that I have stored in a list. I can easily enough get
any column of them using lapply but what I want is to
be able to create a matrix of them. Other than some
kind of brute force looping approach I have drawn a
blank.
Would somebody please suggest something? Thanks
Example.
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
Looking for the perfect gift? Give the gift of Flickr!
______________________________________________ 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.
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Ideally something like this:
======================================================
t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
======================================================
Thanks
--- Erik Iverson <iverson at biostat.wisc.edu> wrote:
What would you want your output matrix to look like given mylist? John Kane wrote:
I have a set of character vectors of uneven length that I have stored in a list. I can easily enough
get
any column of them using lapply but what I want
is to
be able to create a matrix of them. Other than
some
kind of brute force looping approach I have drawn
a
blank.
Would somebody please suggest something? Thanks
Example.
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc",
"silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
Looking for the perfect gift? Give the gift
of Flickr!
______________________________________________ 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.
[[elided trailing spam]]
My thanks to Henrique Dallazuanna and Phil Spector.
Both solutions worked well.
Phil suggested that an alterative to my function would
be
vect1 = sapply(mylist,'[[',1)
and I see that Henrique used `[` in his solution.
Can you point me to some documentation that discusses
these usages. I have seen them before but I have never
actually figured out how to use them.?
Thanks.
Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) < mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
[[elided trailing spam]]
On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
My thanks to Henrique Dallazuanna and Phil Spector. Both solutions worked well. Phil suggested that an alterative to my function would be vect1 = sapply(mylist,'[[',1) and I see that Henrique used `[` in his solution. Can you point me to some documentation that discusses these usages. I have seen them before but I have never actually figured out how to use them.?
See ?Extract
Thanks.
Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) < mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
Looking for the perfect gift? Give the gift of Flickr!
http://www.flickr.com/gift/
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
or the suggestive :) ?"[" b
On Mar 13, 2008, at 2:58 PM, Henrique Dallazuanna wrote:
On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
Can you point me to some documentation that discusses these usages. I have seen them before but I have never actually figured out how to use them.?
See ?Extract
Ah ?Extract. Thanks Unless I'm missing something there is nothing in the Usage or Examples to suggest to a naive reader like me that one can use an unbalanced [ or [[, that is withoug a corresponding ] or ]]. I probably am just not understanding the details.
--- Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
My thanks to Henrique Dallazuanna and Phil
Spector.
Both solutions worked well. Phil suggested that an alterative to my function
would
be vect1 = sapply(mylist,'[[',1) and I see that Henrique used `[` in his solution. Can you point me to some documentation that
discusses
these usages. I have seen them before but I have
never
actually figured out how to use them.?
See ?Extract
Thanks. Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc",
"silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <-
c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) <
mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
Looking for the perfect gift? Give the gift
of Flickr!
-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
A example: x <- rnorm(5) x[3] `[`(x, 3) `[[`(x, 3) x[3:4] `[`(x, 3:4) `[[`(x, 3:4) # Error
On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
Ah ?Extract. Thanks Unless I'm missing something there is nothing in the Usage or Examples to suggest to a naive reader like me that one can use an unbalanced [ or [[, that is withoug a corresponding ] or ]]. I probably am just not understanding the details. --- Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
> > My thanks to Henrique Dallazuanna and Phil
> Spector.
> > Both solutions worked well. > > Phil suggested that an alterative to my function
> would
> > be > > vect1 = sapply(mylist,'[[',1) > > and I see that Henrique used `[` in his solution. > > > > Can you point me to some documentation that
> discusses
> > these usages. I have seen them before but I have
> never
> > actually figured out how to use them.?
> > See ?Extract >
> > > > Thanks. > > > > Problem and solutions > >
>
========================================================
> > mylist <- list(aa=c("cat","peach" ), bb=c("dog",
> > "apple", "iron"),
> > cc = c("rabbit", "orange", "zinc",
> "silk"))
> > myfun <- function(dff) dff[1]
> > vect1 <- unlist(lapply(mylist, myfun))
> >
> > # Desired output
> > t(cbind( c("cat" , "peach" , NA, NA), bbb <-
> c("dog"
> > , "apple" ,"iron", NA),
> > ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
> >
> > # Phil Spector's approach
> > mlen = max(sapply(mylist,length))
> > eqlens = lapply(mylist,function(x)if(length(x) <
> mlen)
> >
> > c(x,rep('',mlen-length(x))) else x)
> > do.call(rbind,eqlens)
> >
> > # "Henrique Dallazuanna" <wwwhsd at gmail.com>
> > # I added the t()
> > t(as.data.frame(lapply(mylist, `[`,
> > 1:max(unlist(lapply(mylist,
> > length))))))
> >
> >
> > Looking for the perfect gift? Give the gift
> of Flickr!
> > > > http://www.flickr.com/gift/ > > > >
> > > -- > Henrique Dallazuanna > Curitiba-Paran?-Brasil > 25? 25' 40" S 49? 16' 22" O >
____________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com.
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Blood ell ! Thanks very much, it does help give a feel for what's happening. I'll have to do some experimenting.
--- Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
A example: x <- rnorm(5) x[3] `[`(x, 3) `[[`(x, 3) x[3:4] `[`(x, 3:4) `[[`(x, 3:4) # Error On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
Ah ?Extract. Thanks Unless I'm missing something there is nothing in
the
Usage or Examples to suggest to a naive reader
like me
that one can use an unbalanced [ or [[, that is withoug a corresponding ] or ]]. I probably am just not understanding the details. --- Henrique Dallazuanna <wwwhsd at gmail.com>
wrote:
> On 13/03/2008, John Kane <jrkrideau at yahoo.ca>
wrote:
> > My thanks to Henrique Dallazuanna and Phil
> Spector.
> > Both solutions worked well. > > Phil suggested that an alterative to my
function
> would
> > be > > vect1 = sapply(mylist,'[[',1) > > and I see that Henrique used `[` in his
solution.
> > > > Can you point me to some documentation that
> discusses
> > these usages. I have seen them before but I
have
> never
> > actually figured out how to use them.?
> > See ?Extract >
> > > > Thanks. > > > > Problem and solutions > >
>
========================================================
> > mylist <- list(aa=c("cat","peach" ),
bb=c("dog",
> > "apple", "iron"),
> > cc = c("rabbit", "orange", "zinc",
> "silk"))
> > myfun <- function(dff) dff[1]
> > vect1 <- unlist(lapply(mylist, myfun))
> >
> > # Desired output
> > t(cbind( c("cat" , "peach" , NA, NA), bbb
<-
> c("dog"
> > , "apple" ,"iron", NA),
> > ccb <- c("rabbit" ,"orange" ,"zinc" ,
"silk" )))
> > > > # Phil Spector's approach > > mlen = max(sapply(mylist,length)) > > eqlens =
lapply(mylist,function(x)if(length(x) <
> mlen)
> >
> > c(x,rep('',mlen-length(x))) else x)
> > do.call(rbind,eqlens)
> >
> > # "Henrique Dallazuanna"
<wwwhsd at gmail.com>
> > # I added the t() > > t(as.data.frame(lapply(mylist, `[`, > > 1:max(unlist(lapply(mylist, > > length)))))) > > > > > > Looking for the perfect gift? Give the
gift
> of Flickr!
> > > > http://www.flickr.com/gift/ > > > >
> > > -- > Henrique Dallazuanna > Curitiba-Paran???-Brasil > 25??? 25' 40" S 49??? 16' 22" O >
____________________________________________________
the web, and bookmark your favourite sites. Download it now at
-- Henrique Dallazuanna Curitiba-Paran???-Brasil 25??? 25' 40" S 49??? 16' 22" O
[[elided trailing spam]]
?"[" ?InternalMethods
x[i,j] is just shorthand for "["(x,i,j) . (AFAIK)**All** operators
(+,-,...,subscripting,...) in R are functions, stemming from its LISP-like
heritage, and can actually called by the usual functional syntax, f(...),
instead of the operator syntax.
Not sure where this is explicitly discussed within R's documentation, but
you can find info on it in V&R's "S Programming", esp. p.24 and 4.3,
"Extracting or replacing coefficients".
No doubt, other S/R books explain it also.
Cheers,
Bert Gunter
Genentech Nonclinical Statistics
47374
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of John Kane
Sent: Thursday, March 13, 2008 11:53 AM
To: Henrique Dallazuanna
Cc: R R-help
Subject: Re: [R] Splitting a set of vectors in a list (Solved )
My thanks to Henrique Dallazuanna and Phil Spector.
Both solutions worked well.
Phil suggested that an alterative to my function would
be
vect1 = sapply(mylist,'[[',1)
and I see that Henrique used `[` in his solution.
Can you point me to some documentation that discusses
these usages. I have seen them before but I have never
actually figured out how to use them.?
Thanks.
Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) < mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
[[elided trailing spam]]
______________________________________________
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.
From: Bert Gunter
?"[" ?InternalMethods x[i,j] is just shorthand for "["(x,i,j) . (AFAIK)**All** operators (+,-,...,subscripting,...) in R are functions, stemming from its LISP-like heritage, and can actually called by the usual functional syntax, f(...), instead of the operator syntax.
That is true even for assignment:
R> "<-"(junk, 1:3)
R> junk
[1] 1 2 3
and "{":
R> "{"(1, 2, 3)
[1] 3
I believe this is in the (draft) R Language Definition, part of the
official manuals that shipped with R.
Andy
Not sure where this is explicitly discussed within R's
documentation, but
you can find info on it in V&R's "S Programming", esp. p.24 and 4.3,
"Extracting or replacing coefficients".
No doubt, other S/R books explain it also.
Cheers,
Bert Gunter
Genentech Nonclinical Statistics
47374
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On
Behalf Of John Kane
Sent: Thursday, March 13, 2008 11:53 AM
To: Henrique Dallazuanna
Cc: R R-help
Subject: Re: [R] Splitting a set of vectors in a list (Solved )
My thanks to Henrique Dallazuanna and Phil Spector.
Both solutions worked well.
Phil suggested that an alterative to my function would
be
vect1 = sapply(mylist,'[[',1)
and I see that Henrique used `[` in his solution.
Can you point me to some documentation that discusses
these usages. I have seen them before but I have never
actually figured out how to use them.?
Thanks.
Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) < mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
[[elided trailing spam]]
______________________________________________ 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. ______________________________________________ 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.
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachme...{{dropped:15}}
--- "Liaw, Andy" <andy_liaw at merck.com> wrote:
From: Bert Gunter
?"[" ?InternalMethods x[i,j] is just shorthand for "["(x,i,j) .
(AFAIK)**All** operators
(+,-,...,subscripting,...) in R are functions,
stemming from
its LISP-like heritage, and can actually called by the usual
functional
syntax, f(...), instead of the operator syntax.
That is true even for assignment: R> "<-"(junk, 1:3) R> junk [1] 1 2
Okay I think I've got this one but
and "{":
R> "{"(1, 2, 3)
this defeats me. I see what it is doing but I have not
the slightest idea why .
I had a look at ?"{" and if I am understanding the
example {2+3; 4+5} what is happening is that anything
within the {} is being executed as separate statments
but I have not the slightest idea of what is happening
when "{"(1, 2, 3) returns 3.
The other thing is, is it worth trying to figure out
what appears to be rather esotheric coding if I can do
the same with more intuitively understood albeit
clumsier code?
I believe this is in the (draft) R Language Definition, part of the official manuals that shipped with R. Andy
Not sure where this is explicitly discussed within
R's
documentation, but you can find info on it in V&R's "S Programming",
esp. p.24 and 4.3,
"Extracting or replacing coefficients". No doubt, other S/R books explain it also. Cheers, Bert Gunter Genentech Nonclinical Statistics 47374 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of John Kane Sent: Thursday, March 13, 2008 11:53 AM To: Henrique Dallazuanna Cc: R R-help Subject: Re: [R] Splitting a set of vectors in a
list (Solved )
My thanks to Henrique Dallazuanna and Phil
Spector.
Both solutions worked well. Phil suggested that an alterative to my function
would
be vect1 = sapply(mylist,'[[',1) and I see that Henrique used `[` in his solution. Can you point me to some documentation that
discusses
these usages. I have seen them before but I have
never
actually figured out how to use them.? Thanks. Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc",
"silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <-
c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) <
mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
[[elided trailing spam]]
______________________________________________ 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.
______________________________________________ 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.
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu - direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.
------------------------------------------------------------------------------
[[elided trailing spam]]
From: John Kane
--- "Liaw, Andy" <andy_liaw at merck.com> wrote:
From: Bert Gunter
?"[" ?InternalMethods x[i,j] is just shorthand for "["(x,i,j) .
(AFAIK)**All** operators
(+,-,...,subscripting,...) in R are functions,
stemming from
its LISP-like heritage, and can actually called by the usual
functional
syntax, f(...), instead of the operator syntax.
That is true even for assignment: R> "<-"(junk, 1:3) R> junk [1] 1 2
Okay I think I've got this one but
and "{":
R> "{"(1, 2, 3)
this defeats me. I see what it is doing but I have not
the slightest idea why .
I had a look at ?"{" and if I am understanding the
example {2+3; 4+5} what is happening is that anything
within the {} is being executed as separate statments
but I have not the slightest idea of what is happening
when "{"(1, 2, 3) returns 3.
The value of "{" is simply the last statement inside. This is basically
the reason why when one writes a function, one can simply type the name
of the object (or the expression) to be returned as the last line,
instead of having to wrap that in return().
The other thing is, is it worth trying to figure out what appears to be rather esotheric coding if I can do the same with more intuitively understood albeit clumsier code?
I do not believe anyone in his/her right mind would write code that way (except those with very developed left brain that can code in LISP-like languages). To me the point is more about understanding what these things do, so you can use them in perhaps some creative ways (but not to the point of abusing it, of course). Andy
I believe this is in the (draft) R Language Definition, part of the official manuals that shipped with R. Andy
Not sure where this is explicitly discussed within
R's
documentation, but you can find info on it in V&R's "S Programming",
esp. p.24 and 4.3,
"Extracting or replacing coefficients". No doubt, other S/R books explain it also. Cheers, Bert Gunter Genentech Nonclinical Statistics 47374 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of John Kane Sent: Thursday, March 13, 2008 11:53 AM To: Henrique Dallazuanna Cc: R R-help Subject: Re: [R] Splitting a set of vectors in a
list (Solved )
My thanks to Henrique Dallazuanna and Phil
Spector.
Both solutions worked well. Phil suggested that an alterative to my function
would
be vect1 = sapply(mylist,'[[',1) and I see that Henrique used `[` in his solution. Can you point me to some documentation that
discusses
these usages. I have seen them before but I have
never
actually figured out how to use them.? Thanks. Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc",
"silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <-
c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" )))
# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) <
mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))
[[elided trailing spam]]
______________________________________________ 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.
______________________________________________ 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.
-------------------------------------------------------------- ----------------
Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu - direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.
-------------------------------------------------------------- ----------------
Looking for the perfect gift? Give the gift of Flickr!
http://www.flickr.com/gift/
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachme...{{dropped:15}}
--- "Liaw, Andy" <andy_liaw at merck.com> wrote:
From: John Kane
--- "Liaw, Andy" <andy_liaw at merck.com> wrote:
From: Bert Gunter
?"[" ?InternalMethods x[i,j] is just shorthand for "["(x,i,j) .
(AFAIK)**All** operators
(+,-,...,subscripting,...) in R are functions,
stemming from
its LISP-like heritage, and can actually called by the usual
functional
syntax, f(...), instead of the operator syntax.
That is true even for assignment: R> "<-"(junk, 1:3) R> junk [1] 1 2
Okay I think I've got this one but
and "{":
R> "{"(1, 2, 3)
this defeats me. I see what it is doing but I have
not
the slightest idea why .
I had a look at ?"{" and if I am understanding the
example {2+3; 4+5} what is happening is that
anything
within the {} is being executed as separate
statments
but I have not the slightest idea of what is
happening
when "{"(1, 2, 3) returns 3.
The value of "{" is simply the last statement
inside. This is basically
the reason why when one writes a function, one can
simply type the name
of the object (or the expression) to be returned as
the last line,
instead of having to wrap that in return().
Very simple when explained. Thanks very much.
So then, all that } is doing is signifiying the end of
{ ?
The other thing is, is it worth trying to figure
out
what appears to be rather esotheric coding if I
can do
the same with more intuitively understood albeit clumsier code?
I do not believe anyone in his/her right mind would write code that way (except those with very developed left brain that can code in LISP-like languages). To me the point is more about understanding what these things do, so you can use them in perhaps some creative ways (but not to the point of abusing it, of course). Andy
Given some of the responses in R-help a naive user like should perhaps be excused for thinking that some of the things are writen is a slightly esorteric manner.
I believe this is in the (draft) R Language Definition, part of the official manuals that shipped with R. Andy
Not sure where this is explicitly discussed
within
R's
documentation, but you can find info on it in V&R's "S
Programming",
esp. p.24 and 4.3,
"Extracting or replacing coefficients". No doubt, other S/R books explain it also. Cheers, Bert Gunter Genentech Nonclinical Statistics 47374 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of John Kane Sent: Thursday, March 13, 2008 11:53 AM To: Henrique Dallazuanna Cc: R R-help Subject: Re: [R] Splitting a set of vectors in
a
list (Solved )
My thanks to Henrique Dallazuanna and Phil
Spector.
Both solutions worked well. Phil suggested that an alterative to my
function
would
be vect1 = sapply(mylist,'[[',1) and I see that Henrique used `[` in his
solution.
Can you point me to some documentation that
discusses
these usages. I have seen them before but I
have
never
actually figured out how to use them.? Thanks. Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ),
bb=c("dog",
"apple", "iron"),
cc = c("rabbit", "orange", "zinc",
"silk"))
myfun <- function(dff) dff[1]
vect1 <- unlist(lapply(mylist, myfun))
# Desired output
t(cbind( c("cat" , "peach" , NA, NA), bbb <-
c("dog"
, "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" , "silk"
)))
# Phil Spector's approach mlen = max(sapply(mylist,length)) eqlens = lapply(mylist,function(x)if(length(x)
<
mlen)
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)
# "Henrique Dallazuanna" <wwwhsd at gmail.com>
# I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
length))))))