我是R和stackoverflow的新手,我需要你在R中重新构建csv数据的帮助,我正在读取一个文件,其中每行代表图形x的可变n点,y具有下一个结构:
code x y x y x y x y
1 1 0 2 2 3 3 4 5 // 1rst graphic with 4 points
2 1 1 2 3 // 2nd graphic with only 2 points
3 0 2 3 5 5 12 10 23 // 3rd graphic with 4 points
输出cvs estructure我需要这样的结构:
code x y
1 1 0
1 2 2
1 3 3
1 4 5
2 1 1
2 2 3
3 0 2
3 3 5
3 5 12
3 10 23
这是否可以只使用read.csv以及如何使用? 感谢任何帮助,谢谢!
答案 0 :(得分:1)
正如里卡多在评论中指出的那样,read.csv
无法直接做到这一点。相反,您可以读取数据,然后使用reshape
来获取输出。我添加了一些额外的步骤来删除具有NA
值的行,依此类推,但这不是完全必要的。
您提供的数据。您提到它是CSV,因此您可能正在使用read.csv
而不是read.table
。
out <- read.table(text = "code x y x y x y x y
1 1 0 2 2 3 3 4 5 // 1rst graphic with 4 points
2 1 1 2 3 // 2nd graphic with only 2 points
3 0 2 3 5 5 12 10 23 // 3rd graphic with 4 points",
fill = TRUE, comment.char = "/", header = TRUE)
更改第一个“x”和“y”对的名称,使其附加“.0”。
names(out)[2:3] <- c("x.0", "y.0")
out
# code x.0 y.0 x.1 y.1 x.2 y.2 x.3 y.3
# 1 1 1 0 2 2 3 3 4 5
# 2 2 1 1 2 3 NA NA NA NA
# 3 3 0 2 3 5 5 12 10 23
使用reshape
获取所需的数据表单。
outL <- reshape(out, direction = "long", idvar="code", varying = 2:ncol(out))
outL <- outL[order(outL$code), ]
outL[complete.cases(outL), -2]
# code x y
# 1.0 1 1 0
# 1.1 1 2 2
# 1.2 1 3 3
# 1.3 1 4 5
# 2.0 2 1 1
# 2.1 2 2 3
# 3.0 3 0 2
# 3.1 3 3 5
# 3.2 3 5 12
# 3.3 3 10 23