原始数据框:
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
有一列名为“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"
我想将列“dt”转换为多个列,如:
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
我该怎么办?
答案 0 :(得分:2)
如果z$dt
中的数据都具有相同的长度,则以下内容应该有效:
x <- do.call(rbind, z$dt)
colnames(x) <- LETTERS[1:ncol(x)]
cbind(z[c("sg", "time")], x)
答案 1 :(得分: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 = ", ")
答案 2 :(得分: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