简体   繁体   English

从包含元素和空格的单个字符串创建R数据帧

[英]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.

相关问题 从数据框中的前 2 个元素创建一个字符串并添加到 R 中的新列 - create a string from first 2 elements in dataframe and add to new column in R 创建仅包含R中2个数据帧中匹配数据的数据帧 - Create dataframe containing only matching data from 2 dataframes in R 在R中从头开始创建具有重复字符串的数据框 - Create dataframe with repeating string from scratch in R 在 dataframe 中创建列,R 中的字符串不存在 - create columns in dataframe with absent from string in R 从另一个 dataframe 中的 a.character 列创建 dataframe 包含 R 中的元素列表 - creating a dataframe from a.character column in another dataframe containing a list of elements in R R。 使用向量中元素的条件组合创建 dataframe - R. Create dataframe with conditional combinations of elements from vector 从 R 中的 dataframe 列中提取唯一的字符串元素 - Extract unique string elements from dataframe column in R 从 r 中的字符串中删除空格的问题 - Problem removing whitespace from a string in r 创建一个包含另一个数据框列中唯一值计数的 R 数据框 - Create an R dataframe containing the counts of unique values in another dataframe column 您可以从包含顶点坐标的 dataframe 在 R 中创建多个多边形吗? - Can you create multiple polygons in R from a dataframe containing the vertices' coordinates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM