我有一个看起来像
的数据集 Id value
abc one
abc two
abc three
abc four
abc five
def two
def three
def three
def four
def seven
def eight
def ten
def eleven
我想获得以下矩阵,
id value1 value2 value3 value4 value5 value6 value7 value8 value9 value10 value11 value12 value13
abc one two three four five 0 0 0 0 0 0 0 0
def two three four seven eight ten eleven 0 0 0 0 0 0
我尝试使用dcast,但它没有填充矩阵中的值,
result <- dcast(data,id~value,value.var = "value",fill = "value")
任何其他想法?
答案 0 :(得分:0)
试试这个:
x = read.table(text = "
Id value
abc one
abc two
abc three
abc four
abc five
def two
def three
def three
def four
def seven
def eight
def ten
def eleven", header = T)
library(dplyr)
library(tidyr)
# spreading the values
x = x[!duplicated(x), ] # remove duplicate
y = x %>%
group_by(Id) %>%
mutate(row.code = 1:n()) %>%
spread(row.code, value)
# adding the extra NA columns and names
y = cbind(y, matrix(rep(NA, nrow(y) * (nrow(x) - ncol(y))), nrow = nrow(y)))
names(y)[-1] = paste0("value", 1:(nrow(x) - 1))
希望它有所帮助。