[英]Create R dataframe from single string containing elements and whitespace
I have string that looks like this 我有一个看起来像这样的字符串
string <- "\n\t\t\t1,2\n\t\t\t3,4\n\t\t"
And I need to create following data frame 我需要创建以下数据框
1 2
3 4
One way is to use strsplit function, but it feels that there must be much better way of doing it. 一种方法是使用strsplit函数,但感觉必须有更好的方法。 Any help will be appreciated.
任何帮助将不胜感激。
You can convert a character string to a thing that behaves a bit like a file with textConnection
and then feed it to read.csv
: 您可以使用
textConnection
将字符串转换为行为类似于文件的textConnection
,然后将其提供给read.csv
:
> read.csv(textConnection(string),head=FALSE)
V1 V2
1 1 2
2 3 4
3 NA NA
the stray tabs after the last newline produce that last line of NA values, you'd need to take care of those some way. 最后一个换行符之后的杂散标签会产生最后一行NA值,您需要采取一些措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.