0

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?

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
chenchenmomo
  • 233
  • 1
  • 3
  • 16

3 Answers3

3

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)
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • It looks like what I mean, but it's not real columns, if I want to select columns 'x[,A]' there is a error 'Error: object 'A' not found'. How can I convert it to real columns of data frame? – chenchenmomo Jul 29 '14 at 13:14
  • @Chenlu, did you try quoting "A"? Also, don't forget to store the output of `cbind` as a new object. – A5C1D2H2I1M1N2O1R2T1 Jul 29 '14 at 13:16
2
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 = ", ")
0
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
jfreels
  • 123
  • 4