我有一个看起来像
的Csv文件name day value
anji 1 10
anji 2 6
aru 1 8
aru 2 5
我想将新csv文件的输出作为
name day1 day2
anji 10 6
aru 8 5
答案 0 :(得分:1)
您可以使用spread
中的tidyr
或来自R的reshape
。
xy <- read.table(text = "name day value
anji 1 10
anji 2 6
aru 1 8
aru 2 5", header = TRUE)
library(tidyr)
out <- spread(data = xy, key = day, value = value)
# or
out <- reshape(xy, idvar = "name", timevar = "day", direction = "wide")
name 1 2
1 anji 10 6
2 aru 8 5
write.table(out, file = "myfile.csv", sep = ",",
col.names = TRUE, row.names = FALSE)