Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split all columns in one data frame and create two data frames in R

I have a single data frame (let's call it df) that looks like this:

col1 <- c("1/10", "2/30", "1/40", "3/23", "0/17", "7/14")
col2 <- c("2/44", "0/13", "4/55", "6/43", "0/19", "2/34")
col3 <- c("0/36", "0/87", "3/11", "2/12", "4/33", "0/12")
col4 <- c("1/76", "2/65", "2/21", "5/0", "2/26", "1/52")

df <- data.frame(col1,col2,col3,col4)

GOAL: In each cell there is are two numbers separated by a "/". Create two data frames: 1 data frame with the the LEFT number and another data frame with the RIGHT number.

The end result would ideally look like this:

df.left.numbers:

  col1 col2 col3 col4
  1    2    0    1
  2    0    0    2
  1    4    3    2
  3    6    2    5
  0    0    4    2
  7    2    0    1

df.right.numbers:

  col1 col2 col3 col4
  10   44   36   76
  30   13   87   65
  40   55   11   21
  23   43   12   0
  17   19   33   26
  14   34   12   53

I've used strsplit() but that is for 1 column splitting into two within ONE data frame. I also tried the separate() function in the tidyr package however that requires the name of a given column. I am iterating through all of them. I suppose I could write a loop, however I was wondering if anyone had an easier way of making this happen!

Thanks!!

like image 861
Sheila Avatar asked Dec 10 '22 12:12

Sheila


2 Answers

Try this:

require(data.table)
lapply(split(unlist(
         lapply(df,tstrsplit,"/"),recursive=FALSE),c("Left","Right")),
          as.data.frame)

#$Right
#  col12 col22 col32 col42
#1    10    44    36    76
#2    30    13    87    65
#3    40    55    11    21
#4    23    43    12     0
#5    17    19    33    26
#6    14    34    12    52

#$Left
#  col11 col21 col31 col41
#1     1     2     0     1
#2     2     0     0     2
#3     1     4     3     2
#4     3     6     2     5
#5     0     0     4     2
#6     7     2     0     1
like image 117
nicola Avatar answered Jan 18 '23 02:01

nicola


Not very elegant, but it is short and it works...

col1 <- c("1/10", "2/30", "1/40", "3/23", "0/17", "7/14")
col2 <- c("2/44", "0/13", "4/55", "6/43", "0/19", "2/34")
col3 <- c("0/36", "0/87", "3/11", "2/12", "4/33", "0/12")
col4 <- c("1/76", "2/65", "2/21", "5/0", "2/26", "1/52")

df <- data.frame(col1,col2,col3,col4,stringsAsFactors = FALSE)

dfLeft <- as.data.frame(lapply(df,function(x) gsub("\\/.+","",x)))
dfRight <- as.data.frame(lapply(df,function(x) gsub(".+\\/","",x)))
like image 20
Andrew Gustar Avatar answered Jan 18 '23 02:01

Andrew Gustar