ls is a list of character vectors created by strsplit() I want to concatenate the 1st 4 character elements of each list item as a new vector called file. I admit to being confused about list syntax even after numerous readings. Here's what I tried: ls <- list(c("Focused", "10k", "A12", "t04.tif", "+", "µm"), c("Focused", "10k", "A12", "t08.tif", "+", "µm"), c("Focused", "10k", "A12", "t12.tif", "+", "µm"), c("Focused", "10k", "A12", "t16.tif", "+", "µm"), c("Focused", "10k", "A12", "t20.tif", "+", "µm"), c("Focused", "10k", "A12", "t24.tif", "+", "µm"), c("Focused", "10k", "A12", "t36.tif", "+", "µm"), c("Focused", "10k", "A12", "t48.tif", "+", "µm"), c("Focused", "10k", "B12", "t04.tif", "+", "µm"), c("Focused", "10k", "B12", "t08.tif", "+", "µm")) # Test the waters with one element cat(unlist(ls[1])[1:4]) # WHY DOES THE COMMAND PROMPT NOT APPEAR ON NEXT LINE AS USUAL??? # Appears to work except for command prompt glitch # Attempts to use tapply() don't get me anywhere file <- tapply(unlist(ls), list(1:length(unlist(ls))), cat(unlist(ls[1])[1:4])) I'm grateful for an approach to putting my vector together, but I'd also love to understand the headache I've apparently given the command parser. I'm apparently doing some "no no". Thanks, Rob> R.Version()$platform [1] "i386-pc-mingw32" $arch [1] "i386" $os [1] "mingw32" $system [1] "i386, mingw32" $status [1] "" $major [1] "2" $minor [1] "12.1" $year [1] "2010" $month [1] "12" $day [1] "16" $`svn rev` [1] "53855" $language [1] "R" $version.string [1] "R version 2.12.1 (2010-12-16)" [[alternative HTML version deleted]]
Hi Robert, You might try do.call(rbind, lapply(yourlist, "[", 1:4)) and then write the resulting file using write.table(...). Best, Jorge On Sun, Feb 20, 2011 at 11:13 AM, Robert Baer <> wrote:> ls is a list of character vectors created by strsplit() > > I want to concatenate the 1st 4 character elements of each list item as a > new vector called file. I admit to being confused about list syntax even > after numerous readings. > > Here's what I tried: > > ls <- list(c("Focused", "10k", "A12", "t04.tif", "+", "µm"), c("Focused", > "10k", "A12", "t08.tif", "+", "µm"), c("Focused", "10k", "A12", > "t12.tif", "+", "µm"), c("Focused", "10k", "A12", "t16.tif", > "+", "µm"), c("Focused", "10k", "A12", "t20.tif", "+", "µm"), > c("Focused", "10k", "A12", "t24.tif", "+", "µm"), c("Focused", > "10k", "A12", "t36.tif", "+", "µm"), c("Focused", "10k", > "A12", "t48.tif", "+", "µm"), c("Focused", "10k", "B12", > "t04.tif", "+", "µm"), c("Focused", "10k", "B12", "t08.tif", > "+", "µm")) > > # Test the waters with one element > cat(unlist(ls[1])[1:4]) # WHY DOES THE COMMAND PROMPT NOT APPEAR ON > NEXT LINE AS USUAL??? > > # Appears to work except for command prompt glitch > > # Attempts to use tapply() don't get me anywhere > file <- tapply(unlist(ls), list(1:length(unlist(ls))), > cat(unlist(ls[1])[1:4])) > > I'm grateful for an approach to putting my vector together, but I'd also > love to understand the headache I've apparently given the command parser. > I'm apparently doing some "no no". > > Thanks, > > Rob > > > R.Version() > $platform > [1] "i386-pc-mingw32" > > $arch > [1] "i386" > > $os > [1] "mingw32" > > $system > [1] "i386, mingw32" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "12.1" > > $year > [1] "2010" > > $month > [1] "12" > > $day > [1] "16" > > $`svn rev` > [1] "53855" > > $language > [1] "R" > > $version.string > [1] "R version 2.12.1 (2010-12-16)" > > > [[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. > >[[alternative HTML version deleted]]
On Feb 20, 2011, at 11:13 AM, Robert Baer wrote:> ls is a list of character vectors created by strsplit() > > I want to concatenate the 1st 4 character elements of each list > item as a new vector called file. I admit to being confused about > list syntax even after numerous readings. > > Here's what I tried: > > ls <- list(c("Focused", "10k", "A12", "t04.tif", "+", "?m"), > c("Focused", > "10k", "A12", "t08.tif", "+", "?m"), c("Focused", "10k", "A12", > "t12.tif", "+", "?m"), c("Focused", "10k", "A12", "t16.tif", > "+", "?m"), c("Focused", "10k", "A12", "t20.tif", "+", "?m"), > c("Focused", "10k", "A12", "t24.tif", "+", "?m"), c("Focused", > "10k", "A12", "t36.tif", "+", "?m"), c("Focused", "10k", > "A12", "t48.tif", "+", "?m"), c("Focused", "10k", "B12", > "t04.tif", "+", "?m"), c("Focused", "10k", "B12", "t08.tif", > "+", "?m"))Perhaps: > sapply(ls1, paste, collapse="") [1] "Focused10kA12t04.tif+?m" "Focused10kA12t08.tif+?m" [3] "Focused10kA12t12.tif+?m" "Focused10kA12t16.tif+?m" [5] "Focused10kA12t20.tif+?m" "Focused10kA12t24.tif+?m" [7] "Focused10kA12t36.tif+?m" "Focused10kA12t48.tif+?m" [9] "Focused10kB12t04.tif+?m" "Focused10kB12t08.tif+?m" (I changed the name and will not illustrate its assignment to "file". It is generally considered poor programming practice to use function names for variable objects.) -- David.> > # Test the waters with one element > cat(unlist(ls[1])[1:4]) # WHY DOES THE COMMAND PROMPT NOT > APPEAR ON NEXT LINE AS USUAL??? > > # Appears to work except for command prompt glitch > > # Attempts to use tapply() don't get me anywhere > file <- tapply(unlist(ls), list(1:length(unlist(ls))), > cat(unlist(ls[1])[1:4])) > > I'm grateful for an approach to putting my vector together, but I'd > also love to understand the headache I've apparently given the > command parser. I'm apparently doing some "no no". > > Thanks, > > Rob > >> R.Version() > $platform > [1] "i386-pc-mingw32" > > $arch > [1] "i386" > > $os > [1] "mingw32" > > $system > [1] "i386, mingw32" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "12.1" > > $year > [1] "2010" > > $month > [1] "12" > > $day > [1] "16" > > $`svn rev` > [1] "53855" > > $language > [1] "R" > > $version.string > [1] "R version 2.12.1 (2010-12-16)" > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT