Miha Trošt
Miha Trošt

Reputation: 2022

Split vector of string elements into list

I have a vector:

my_vec <- 
c("Iceland", "06/2010,60% ,38% ,1% ,1% ,0% ", "11/2010,63% ,36% ,1% ,0% ,0% ", 
"05/2011,59% ,38% ,2% ,1% ,0% ", "11/2011,56% ,40% ,3% ,0% ,1% ", 
"05/2012,60% ,36% ,2% ,2% ,0% ", "11/2012,59% ,40% ,1% ,0% ,0% ", 
"05/2013,60% ,38% ,1% ,0% ,1% ", "11/2013,55% ,43% ,2% ,0% ,0% ", 
"06/2014,59% ,39% ,2% ,0% ,0% ", "Montenegro", "05/2011,11% ,41% ,36% ,11% ,1% ", 
"11/2011,12% ,43% ,32% ,12% ,1% ", "05/2012,8% ,35% ,38% ,14% ,5% ", 
"11/2012,9% ,35% ,34% ,18% ,4% ", "05/2013,10% ,39% ,32% ,16% ,3% ", 
"11/2013,10% ,34% ,36% ,19% ,1% ", "06/2014,15% ,47% ,27% ,11% ,0% ", 
"Republic of Serbia ", "05/2012,3% ,31% ,43% ,20% ,3% ", "11/2012,5% ,28% ,43% ,21% ,3% ", 
"05/2013,6% ,29% ,44% ,18% ,3% ", "11/2013,7% ,34% ,39% ,18% ,2% ", 
"06/2014,11% ,40% ,33% ,16% ")

The vector contains both country name and some values that are comma delimited. I would like to split the vector to list by country name.

I tried:

library(stringr)

split(my_vec, which(str_detect(my_vec, "[aeiou]")))

but the output is not correct:

$`1`
[1] "Iceland"                         "05/2011,59% ,38% ,2% ,1% ,0% "  
[3] "11/2012,59% ,40% ,1% ,0% ,0% "   "06/2014,59% ,39% ,2% ,0% ,0% "  
[5] "11/2011,12% ,43% ,32% ,12% ,1% " "05/2013,10% ,39% ,32% ,16% ,3% "
[7] "Republic of Serbia "             "05/2013,6% ,29% ,44% ,18% ,3% " 

$`11`
[1] "06/2010,60% ,38% ,1% ,1% ,0% "   "11/2011,56% ,40% ,3% ,0% ,1% "  
[3] "05/2013,60% ,38% ,1% ,0% ,1% "   "Montenegro"                     
[5] "05/2012,8% ,35% ,38% ,14% ,5% "  "11/2013,10% ,34% ,36% ,19% ,1% "
[7] "05/2012,3% ,31% ,43% ,20% ,3% "  "11/2013,7% ,34% ,39% ,18% ,2% " 

$`19`
[1] "11/2010,63% ,36% ,1% ,0% ,0% "   "05/2012,60% ,36% ,2% ,2% ,0% "  
[3] "11/2013,55% ,43% ,2% ,0% ,0% "   "05/2011,11% ,41% ,36% ,11% ,1% "
[5] "11/2012,9% ,35% ,34% ,18% ,4% "  "06/2014,15% ,47% ,27% ,11% ,0% "
[7] "11/2012,5% ,28% ,43% ,21% ,3% "  "06/2014,11% ,40% ,33% ,16% "  

Every list element should be the country name.

Upvotes: 1

Views: 115

Answers (3)

Thomas
Thomas

Reputation: 44525

Updated:

u <- unlist(gregexpr("^[[:alpha:]]", my_vec))
w <- which(u==1)
s <- setNames(split(my_vec[-w], cumsum(u + 1)[-w]), my_vec[w])

Result:

> s
$Iceland
[1] "06/2010,60% ,38% ,1% ,1% ,0% " "11/2010,63% ,36% ,1% ,0% ,0% "
[3] "05/2011,59% ,38% ,2% ,1% ,0% " "11/2011,56% ,40% ,3% ,0% ,1% "
[5] "05/2012,60% ,36% ,2% ,2% ,0% " "11/2012,59% ,40% ,1% ,0% ,0% "
[7] "05/2013,60% ,38% ,1% ,0% ,1% " "11/2013,55% ,43% ,2% ,0% ,0% "
[9] "06/2014,59% ,39% ,2% ,0% ,0% "

$Montenegro
[1] "05/2011,11% ,41% ,36% ,11% ,1% " "11/2011,12% ,43% ,32% ,12% ,1% "
[3] "05/2012,8% ,35% ,38% ,14% ,5% "  "11/2012,9% ,35% ,34% ,18% ,4% " 
[5] "05/2013,10% ,39% ,32% ,16% ,3% " "11/2013,10% ,34% ,36% ,19% ,1% "
[7] "06/2014,15% ,47% ,27% ,11% ,0% "

$`Republic of Serbia `
[1] "05/2012,3% ,31% ,43% ,20% ,3% " "11/2012,5% ,28% ,43% ,21% ,3% "
[3] "05/2013,6% ,29% ,44% ,18% ,3% " "11/2013,7% ,34% ,39% ,18% ,2% "
[5] "06/2014,11% ,40% ,33% ,16% "

You can also easily convert this into a list of data.frames (per @AnandaMahto's answer):

> lapply(s, function(x) read.csv(text=x))
$Iceland
  X06.2010 X60. X38. X1. X1..1 X0.
1  11/2010 63%  36%  1%    0%  0% 
2  05/2011 59%  38%  2%    1%  0% 
3  11/2011 56%  40%  3%    0%  1% 
4  05/2012 60%  36%  2%    2%  0% 
5  11/2012 59%  40%  1%    0%  0% 
6  05/2013 60%  38%  1%    0%  1% 
7  11/2013 55%  43%  2%    0%  0% 
8  06/2014 59%  39%  2%    0%  0% 

$Montenegro
  X05.2011 X11. X41. X36. X11..1 X1.
1  11/2011 12%  43%  32%    12%  1% 
2  05/2012  8%  35%  38%    14%  5% 
3  11/2012  9%  35%  34%    18%  4% 
4  05/2013 10%  39%  32%    16%  3% 
5  11/2013 10%  34%  36%    19%  1% 
6  06/2014 15%  47%  27%    11%  0% 

$`Republic of Serbia `
  X05.2012  X3. X31. X43. X20. X3..1
1  11/2012  5%  28%  43%  21%    3% 
2  05/2013  6%  29%  44%  18%    3% 
3  11/2013  7%  34%  39%  18%    2% 
4  06/2014 11%  40%  33%  16%       

Upvotes: 0

akrun
akrun

Reputation: 887048

You could try:

 indx <- grep("^[A-Za-z ]", my_vec) #create the index of country names from the list

indx2<- diff(c(indx, length(my_vec)+1))-1 #create another index to replicate the country names

split the vector without country names with country names that are replicated

split(my_vec[-indx], rep(my_vec[indx], indx2))
#$Iceland
#[1] "06/2010,60% ,38% ,1% ,1% ,0% " "11/2010,63% ,36% ,1% ,0% ,0% "
#[3] "05/2011,59% ,38% ,2% ,1% ,0% " "11/2011,56% ,40% ,3% ,0% ,1% "
#[5] "05/2012,60% ,36% ,2% ,2% ,0% " "11/2012,59% ,40% ,1% ,0% ,0% "
#[7] "05/2013,60% ,38% ,1% ,0% ,1% " "11/2013,55% ,43% ,2% ,0% ,0% "
#[9] "06/2014,59% ,39% ,2% ,0% ,0% "

#$Montenegro
#[1] "05/2011,11% ,41% ,36% ,11% ,1% " "11/2011,12% ,43% ,32% ,12% ,1% "
#[3] "05/2012,8% ,35% ,38% ,14% ,5% "  "11/2012,9% ,35% ,34% ,18% ,4% " 
#[5] "05/2013,10% ,39% ,32% ,16% ,3% " "11/2013,10% ,34% ,36% ,19% ,1% "
#[7] "06/2014,15% ,47% ,27% ,11% ,0% "

#$`Republic of Serbia `
#[1] "05/2012,3% ,31% ,43% ,20% ,3% " "11/2012,5% ,28% ,43% ,21% ,3% "
#[3] "05/2013,6% ,29% ,44% ,18% ,3% " "11/2013,7% ,34% ,39% ,18% ,2% "
#[5] "06/2014,11% ,40% ,33% ,16% "   

To convert it to a list of data.frames

lapply(split(my_vec[-indx], rep(my_vec[indx], indx2)), 
     function(x) read.table(text=x, sep=",", header=F, stringsAsFactors=F, fill=T))

Upvotes: 0

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

This isn't what you asked for, but might be more the direction you're heading. It's too long for a comment, so I thought I would post as an answer.

I've written a function called read.mtable that is a wrapper for a for loop that lets you read data into a list of data.frames (which is what it seems like you have here). It's part of my "SOfun" package on GitHub, so you can install it using:

library(devtools)
install_github("SOfun", "mrdwab") ## for `read.mtable`

With your sample vector, I would use it like this:

read.mtable(textConnection(my_vec), 
            chunkId = "^[[:alpha:]]", 
            header = FALSE, fill = TRUE, 
            sep = ",", strip.white = TRUE)
# $Iceland
#        V1  V2  V3 V4 V5 V6
# 1 06/2010 60% 38% 1% 1% 0%
# 2 11/2010 63% 36% 1% 0% 0%
# 3 05/2011 59% 38% 2% 1% 0%
# 4 11/2011 56% 40% 3% 0% 1%
# 5 05/2012 60% 36% 2% 2% 0%
# 6 11/2012 59% 40% 1% 0% 0%
# 7 05/2013 60% 38% 1% 0% 1%
# 8 11/2013 55% 43% 2% 0% 0%
# 9 06/2014 59% 39% 2% 0% 0%
# 
# $Montenegro
#        V1  V2  V3  V4  V5 V6
# 1 05/2011 11% 41% 36% 11% 1%
# 2 11/2011 12% 43% 32% 12% 1%
# 3 05/2012  8% 35% 38% 14% 5%
# 4 11/2012  9% 35% 34% 18% 4%
# 5 05/2013 10% 39% 32% 16% 3%
# 6 11/2013 10% 34% 36% 19% 1%
# 7 06/2014 15% 47% 27% 11% 0%
# 
# $`Republic of Serbia `
#        V1  V2  V3  V4  V5 V6
# 1 05/2012  3% 31% 43% 20% 3%
# 2 11/2012  5% 28% 43% 21% 3%
# 3 05/2013  6% 29% 44% 18% 3%
# 4 11/2013  7% 34% 39% 18% 2%
# 5 06/2014 11% 40% 33% 16%   

Upvotes: 4

Related Questions