Sam Marshal Sam Marshal - 1 year ago 115
R Question

Split String without losing character- R

I have two columns in a much larger dataframe that I am having difficult splitting. I have used

in past when I was trying to split using a "space", "," or some other delimiter. The hard part here is I don't want to lose any information AND when I split some parts I will end up with missing information. I would like to end up with four columns in the end. Here's a sample of a couple rows of what I have now.

age-gen surv-camp
45M 1LC
9F 0
12M 1AC
67M 1LC

Here is what I would like to ultimately get.

age gen surv camp
45 M 1 LC
9 F 0
12 M 1 AC
67 M 1 LC

I've done quite a lot of hunting around on here and have found a number of responses in Java, C++, html etc., but I haven't found anything that explains how to do this in R and when you have missing data.

I saw this about adding a space between values and then just splitting on the space, but I don't see how this would work 1) with missing data, 2) when I don't have consistent numeric or character values in each row.

Answer Source

We loop through the columns of 'df1' (lapply(df1, ..), create a delimiter after the numeric substring using sub, read the vector as data.frame with read.table, rbind the list of data.frames and change the column names of the output.

res <-, lapply(df1, function(x)
      read.table(text=sub("(\\d+)", "\\1,", x), 
          header=FALSE, sep=",", stringsAsFactors=FALSE)))
colnames(res) <- scan(text=names(df1), sep=".", what="", quiet = TRUE)
#  age gen surv camp
#1  45   M    1   LC
#2   9   F    0     
#3  12   M    1   AC
#4  67   M    1   LC

Or using separate from tidyr

separate(df1, age.gen, into = c("age", "gen"), "(?<=\\d)(?=[A-Za-z])") %>% 
       separate(, into = c("surv", "camp"), "(?<=\\d)(?=[A-Za-z])")
#  age gen surv camp
#1  45   M    1   LC
#2   9   F    0 <NA>
#3  12   M    1   AC
#4  67   M    1   LC


df1 <- structure(list(age.gen = c("45M", "9F", "12M", "67M"), = c("1LC", 
 "0", "1AC", "1LC")), .Names = c("age.gen", ""), 
class = "data.frame", row.names = c(NA, -4L))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download