[R] concatenate vector after strsplit()
Robert Baer
rbaer at atsu.edu
Sun Feb 20 18:43:45 CET 2011
> You might try
>
> do.call(rbind, lapply(yourlist, "[", 1:4))
Thanks, Jorge, but when I tried this I simply got a matrix of character
strings rather than my original list of character strings as in:
> m = do.call(rbind, lapply(ls, "[", 1:4))
> m
[,1] [,2] [,3] [,4]
[1,] "Focused" "10k" "A12" "t04.tif"
[2,] "Focused" "10k" "A12" "t08.tif"
[3,] "Focused" "10k" "A12" "t12.tif"
[4,] "Focused" "10k" "A12" "t16.tif"
[5,] "Focused" "10k" "A12" "t20.tif"
[6,] "Focused" "10k" "A12" "t24.tif"
[7,] "Focused" "10k" "A12" "t36.tif"
[8,] "Focused" "10k" "A12" "t48.tif"
[9,] "Focused" "10k" "B12" "t04.tif"
[10,] "Focused" "10k" "B12" "t08.tif"
>
What I want to end up with is a vector that is the row-wise concatenation of
the strings in each row of your matrix or each element of my original list.
I thought the cat() function should get me part way there, but as I
indicated below, there seem to be a parsing issue of some sort the way I'm
using it.
>
>> 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 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.
>>
>>
>
> [[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.
>
More information about the R-help
mailing list