我有一个类似于以下示例的数据框:
> df <- data.frame(imp = c("Johny", "Johny", "Lisa", "Max"), item = c(5025, 1101, 2057, 1619))
> df
imp item
[1,] "Johny" "5025"
[2,] "Johny" "1101"
[3,] "Lisa" "2057"
[4,] "Max" "1619"
我想为每个都有一个唯一的行user
。最终结果应该是这样的:
> df
imp item1 item2
[1,] "Johny" "5025" "1101"
[2,] "Lisa" "2057" NA
[3,] "Max" "1619" NA
## Add an ID column to distinguish multiple measurements per imp
## There's probably a better way to do this?
df <- do.call(rbind, lapply(
split(df, df$imp),
function(x) {
x$item_id <- seq(nrow(x))
return(x)
}
))
## Then simply use the dcast function from the reshape2 package
df <- dcast(df, imp ~ item_id, value.var='item')
## Tidy up the column names
names(df) <- sub('^(\\d+)$', 'item_\\1', names(df))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)