简体   繁体   中英

How to split a column of list into several columns using R

The original data frame :

     sg                                dt               time
2099     C 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 2014-07-24 16:23:55.2
2100     C 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 2014-07-24 16:23:55.4
2101     C 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 2014-07-24 16:23:55.5
2103     C 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0 2014-07-24 16:23:56.4
2104     C 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 2014-07-24 16:23:56.5
2102     C 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 2014-07-24 16:23:56.7

There is one column named "dt",

> z$dt
[[1]]
 [1] "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "0" "0"

[[2]]
 [1] "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "0" "0"

[[3]]
 [1] "0" "0" "1" "0" "0" "1" "0" "0" "0" "0" "0" "0" "0"

[[4]]
 [1] "0" "0" "0" "1" "0" "1" "0" "0" "0" "0" "1" "0" "0"

[[5]]
 [1] "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0"

[[6]]
 [1] "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "0" "1"

I want convert the column "dt" into multiple columns like:

           sg  A  B  C  D  E  F  G  H  I  G  K  L  M             time
    2099     C 0  0  0  0  0  1  0  0  0  0  0  0  0 2014-07-24 16:23:55.2
    2100     C 0  0  0  0  0  1  0  0  0  0  0  0  0 2014-07-24 16:23:55.4
    2101     C 0  0  1  0  0  1  0  0  0  0  0  0  0 2014-07-24 16:23:55.5
    2103     C 0  0  0  1  0  1  0  0  0  0  1  0  0 2014-07-24 16:23:56.4
    2104     C 0  0  0  0  0  1  0  0  0  0  1  0  0 2014-07-24 16:23:56.5
    2102     C 0  0  0  0  0  1  0  0  0  0  0  0  1 2014-07-24 16:23:56.7

What should I do?

The following should work if the data in z$dt all have the same length:

x <- do.call(rbind, z$dt)
colnames(x) <- LETTERS[1:ncol(x)]
cbind(z[c("sg", "time")], x)
library(tidyverse)

# recreate your data frame
df <- tibble(sg = c(2099,
                    2100,
                    2101,
                    2103,
                    2104,
                    2102),
             dt = list(c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0'),
                       c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0'),
                       c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0'),
                       c('0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0'),
                       c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0'),
                       c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1')),
             time = c("2014-07-24 16:23:55.2",
                      "2014-07-24 16:23:55.4",
                      "2014-07-24 16:23:55.5",
                      "2014-07-24 16:23:56.4",
                      "2014-07-24 16:23:56.5",
                      "2014-07-24 16:23:56.7")) %>%
  as.data.frame()

# split column dt into individual columns
df %>%
  separate(col = dt, into = LETTERS[1:13], sep = ", ")
require(dplyr)
require(tidyr)
x<-data.frame(dt=c('0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0',
                   '0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0',
                   '0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0',
                   '0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0',
                   '0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0',
                   '0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0',
                   '0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1'))
x %>%
  separate(col=dt, into=toupper(letters[1:13]))

#  A B C D E F G H I J K L M
#1 0 0 0 0 0 1 0 0 0 0 0 0 0
#2 0 0 0 0 0 1 0 0 0 0 0 0 0
#3 0 0 0 0 0 1 0 0 0 0 0 0 0
#4 0 0 1 0 0 1 0 0 0 0 0 0 0
#5 0 0 0 1 0 1 0 0 0 0 1 0 0
#6 0 0 0 0 0 1 0 0 0 0 1 0 0
#7 0 0 0 0 0 1 0 0 0 0 0 0 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM