[R] concatenate vector after strsplit()

Robert Baer rbaer at atsu.edu
Sun Feb 20 19:19:51 CET 2011


Thanks Jorge and David.  Both worked.  Looks like I have to read more on "[" 
as an extractor function for lists.  do.call() is also on my list of "low 
level magic' I don't understand. <G>   Appreciate the help!

fil1 = do.call(c, lapply(lapply(ls1, "[", 1:4), paste, sep = "", collapse = 
"-"))
fil2 = sapply(lapply(ls1, "[", 1:4), paste, collapse="-")

>>> ls1 <- 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
>
> ______________________________________________
> 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