[R] concatenate vector after strsplit()

David Winsemius dwinsemius at comcast.net
Sun Feb 20 18:53:46 CET 2011


On Feb 20, 2011, at 12:49 PM, David Winsemius wrote:

>
> 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.)

I suppose it would be better practice on my part to read the question  
more thoroughly:

 > sapply(lapply(ls1, "[", 1:4), paste, collapse="")
  [1] "Focused10kA12t04.tif" "Focused10kA12t08.tif"
  [3] "Focused10kA12t12.tif" "Focused10kA12t16.tif"
  [5] "Focused10kA12t20.tif" "Focused10kA12t24.tif"
  [7] "Focused10kA12t36.tif" "Focused10kA12t48.tif"
  [9] "Focused10kB12t04.tif" "Focused10kB12t08.tif"

> -- 
> 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)"
>>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list