我试图从文本文件中获取一个表,然后将其输出(或将其转换为csv)以便于操作。我看了,在这个任务上找不到任何具体的帮助。
文本文件如下所示:
C# CLA OLA Quiz Exam FinalExam
c1234501 10 20 10 30 30
c1234502 9 10 5 20 20
c1234503 10 17 8 27 27
c1234504 8 14 10 29 15
c1234505 7 18 3 24 27
我想让它像这样输出:
C#,CLA,OLA,Quiz,Exam,FinalExam,
c1234501,10,20,10,30,30,
c1234502,9,10,5,20,20,
c1234503,10,17,8,27,27,
c1234504,8, 14, 10, 29, 15,
c1234505,7, 18, 3, 24, 27,
以逗号作为分隔符。 到目前为止,我已经提出了这个代码:
tab = read.delim(file, header = TRUE)
write.table(tab, file="name_file.csv",sep=",",col.names=TRUE,row.names=FALSE)
但结果不是我认为的结果:
"C.","CLA.OLA.Quiz.Exam.FinalExam"
"c1234501","10 20 10 30 30"
"c1234502"," 9 10 5 20 20"
"c1234503","10 17 8 27 27"
"c1234504 "," 8 14 10 29 15"
"c1234505 "," 7 18 3 24 27"
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
明确表示:
tab <- read.table("name_file.txt", comment="", header=TRUE)
write.csv(tab, "name_file.csv", row.names=FALSE, quote=FALSE)
我知道你没有要求它,但如果你基本上使用R作为文件转换器,通过控制台/使用sed
或类似的东西可能会更方便终奌站。 F.ex:
sed -E 's/[[:blank:]]+/,/g' name_file.txt > name_file.csv