如何使用现有的csv文件值编写csv文件

时间:2016-12-06 10:14:03

标签: javascript java r csv

我有一个看起来像

的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

1 个答案:

答案 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)