0

I need to split the column "leg1" into three columns (v1,v2,v3). The problem is that the amount of blank spaces is different by rows.

> head(data[,2:3])
    name                           leg1
513    1      0.00000  0.00000  0.00000
514    2   -0.00000  -0.00000  -0.00000
515    3      0.00000  0.00000  0.00000
516    4   -0.03467  -0.03848  -0.02331
517    5   -0.00000  -0.00000  -0.00000
518    6   -0.00000  -0.00000  -0.00000

I tried to use gsub or substr, but I couldn`t do it.

Could anyone help me, please?

Thanks in advance!!

Kat
  • 47
  • 5

2 Answers2

2

It may be easier to do this with read.table from base R on the 'leg1' column which will split by the default space

cbind(data[-2], read.table(text = data$leg1, header = FALSE))

-output

  name       V1       V2       V3
1    1  0.00000  0.00000  0.00000
2    2  0.00000  0.00000  0.00000
3    3  0.00000  0.00000  0.00000
4    4 -0.03467 -0.03848 -0.02331
5    5  0.00000  0.00000  0.00000

if there are more spaces, then specify strip.white = TRUE

cbind(data[-2], read.table(text = data$leg1, header = FALSE, strip.white = TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can use str_split_fixed from the stringr package, after first replacing multiple spaces with 1:

data <- data.frame(name = c(1,2,3,4,5))

data$leg1 <- c(
  "0.00000  0.00000  0.00000",
  "-0.00000  -0.00000  -0.00000",
  "0.00000  0.00000  0.00000",
  "-0.03467  -0.03848  -0.02331",
  "-0.00000  -0.00000  -0.00000")

data$leg1 <- gsub("\\s+", " ", str_trim(data$leg1))

str_split_fixed(data$leg1, " ", 3) %>% 
  data.frame() %>% 
  rename(v1 = X1, v2 = X2, v3 = X3) %>% 
  cbind(data, .)

##   name                         leg1       v1       v2        v3
## 1    1    0.00000  0.00000  0.00000  0.00000  0.00000   0.00000
## 2    2 -0.00000  -0.00000  -0.00000 -0.00000 -0.00000  -0.00000
## 3    3    0.00000  0.00000  0.00000  0.00000  0.00000   0.00000
## 4    4 -0.03467  -0.03848  -0.02331 -0.03467 -0.03848  -0.02331
## 5    5 -0.00000  -0.00000  -0.00000 -0.00000 -0.00000  -0.00000
Robert Long
  • 5,722
  • 5
  • 29
  • 50