Home > database >  How to split a dataframe column into two columns
How to split a dataframe column into two columns

Time:12-17

Im looking to see how to split a single data frame column into two columns and replace a symbol for another. For example, if the data frame y has;

X1
NA
1/0:0.82
1/1:1.995
0/1:1.146
NA
1/1:1.995

I want to be able to split the data into two columns like so and substitute the "/" symbol for a".".

X1 X2
NA
1.0 0.82
1.1 1.995
0.1 1.146
NA
1.1 1.995

Whenever I try to use str.split I keep running into errors. Any help would e great.

CodePudding user response:

read.table(text=df$X1, sep=':', fill=T, h=F, dec = '/')
   V1    V2
1  NA      
2 1.0  0.82
3 1.1 1.995
4 0.1 1.146
5  NA      
6 1.1 1.995

If you want columns in respective data.types:

type.convert(read.table(text=df$X1, sep=':', fill=T, h=F, dec = '/'), as.is = TRUE)
   V1    V2
1  NA    NA
2 1.0 0.820
3 1.1 1.995
4 0.1 1.146
5  NA    NA
6 1.1 1.995

df <- structure(list(X1 = c(NA, "1/0:0.82", "1/1:1.995", "0/1:1.146", NA,
                 "1/1:1.995")), class = "data.frame", row.names = c(NA, -6L))

CodePudding user response:

You may use separate to split data on : into two columns and replace / to . in the first column. Using data from @Onyambu -

library(dplyr)
library(tidyr)

df <- df %>%
  separate(X1, c('X1', 'X2'), sep = ':') %>%
  mutate(X1 = sub('/', '.', X1)) %>%
  type.convert(as.is = TRUE)

df

#   X1    X2
#1  NA    NA
#2 1.0 0.820
#3 1.1 1.995
#4 0.1 1.146
#5  NA    NA
#6 1.1 1.995

CodePudding user response:

Using strsplit

d
[1] NA          "1/0:0.82"  "1/1:1.995" "0/1:1.146" NA          "1/1:1.995"

data.frame( unname( t(data.frame( strsplit(d, "/") )) ) )
    X1      X2
1 <NA>    <NA>
2    1  0:0.82
3    1 1:1.995
4    0 1:1.146
5 <NA>    <NA>
6    1 1:1.995
  • Related