Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split word in column in R

Tags:

r

I have a data frame with multiple columns in R. I want to split the "age" column into two column, each with one string in it.

         fas       value age colony
   1:  C12:0 0.002221915  LO   7_13
   2:  C13:0 0.000770179  LO   7_13
   3:  C14:0 0.004525352  LO   7_13
   4:  C15:0 0.000738928  LO   7_13
   5: C16:1a 0.002964627  LO   7_13

Output:

         fas           value size age colony
       1:  C12:0 0.002221915    L   O   7_13
       2:  C13:0 0.000770179    L   O   7_13
       3:  C14:0 0.004525352    L   O   7_13
       4:  C15:0 0.000738928    L   O   7_13
       5: C16:1a 0.002964627    L   O   7_13

I tried:

data_frame<-str_split_fixed(df$age, "", 2)
like image 541
Luker354 Avatar asked Feb 21 '21 16:02

Luker354


People also ask

How do I split text into data in R?

Use the split() function in R to split a vector or data frame. Use the unsplit() method to retrieve the split vector or data frame.

How do you split a variable in R?

The split() function in R can be used to split data into groups based on factor levels. This function uses the following basic syntax: split(x, f, …)

How do you split a delimiter in R?

Use str_split to Split String by Delimiter in R Alternatively, the str_split function can also be utilized to split string by delimiter. str_split is part of the stringr package. It almost works in the same way as strsplit does, except that str_split also takes regular expressions as the pattern.

How do I subset a column in R?

Subset a Data Frame with Base R Extract[] If subsetting is done by only rows or only columns, then leave the other value blank. For example, to subset the d data frame only by rows, the general form reduces to d[rows,] . Similarly, to subset only by columns, d[,cols] .


2 Answers

With base R:

df$size <- substr(df$age,1,1)
df$age  <- substr(df$age,2,2)

And to get the result in the column order you specified:

df[,c("fas","value","age","size","colony")]
     fas       value age size colony
1  C12:0 0.002221915   O    L   7_13
2  C13:0 0.000770179   O    L   7_13
3  C14:0 0.004525352   O    L   7_13
4  C15:0 0.000738928   O    L   7_13
5 C16:1a 0.002964627   O    L   7_13
like image 110
Waldi Avatar answered Oct 24 '22 01:10

Waldi


You can use suband backreference:

df$age <- sub("(^\\w)(\\w$)", "\\1", df$age)
df$size <- sub("(^\\w)(\\w$)", "\\2", df$age)
like image 8
Chris Ruehlemann Avatar answered Oct 24 '22 03:10

Chris Ruehlemann