需要将逗号分开的值转换为R数据框架
我有一个看起来像这样的数据文件(TXT文件): 输入:aviles.txt 控制0.53、0.36、0.20,-0.37,-0.60,-0.64,-0.68,-1.27 膝盖0.73、0.31、0.03,-0.29,-0.56,-0.96,-1.61 眼睛-0.78,-0.86,-1.35,-1.48,-1.52,-2.04,-2.83 注意:每行的第一个值之后没有逗号。
数据框中的预期输出: 控制膝盖的眼睛 0.53 0.73 -0.78 0.36 0.31 -0.86 0.2 0.03 -1.35 -0.37 -0.29 -1.48 -0.6 -0.56 -1.52 -0.64 -0.96 -2.04 -0.68 -1.61 -2.83 -1.27
如何使用R进行操作?
I have a data file (txt file) that looks like this:
input: trials.txt
Control 0.53, 0.36, 0.20, -0.37, -0.60, -0.64, -0.68, -1.27
Knees 0.73, 0.31, 0.03, -0.29, -0.56, -0.96, -1.61
Eyes -0.78, -0.86, -1.35, -1.48, -1.52, -2.04, -2.83
Note: there is no comma after the first value in each row.
Expected Output in data frame:
Control Knees Eyes
0.53 0.73 -0.78
0.36 0.31 -0.86
0.2 0.03 -1.35
-0.37 -0.29 -1.48
-0.6 -0.56 -1.52
-0.64 -0.96 -2.04
-0.68 -1.61 -2.83
-1.27
How do I do that using R?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
.txt
使用readlines
读取文件,然后使用read.csv
在删除初始单词以读取为data.frame。定界符,
,t
ranspose并设置列名称,并使用字符词提取的- 输出
或稍微更容易的选项是在
上创建
,
第一个空间,使用read.csv
读取We may read the
.txt
file withreadLines
and then useread.csv
after removing the initial word to read as data.frame making use of the delimiter,
,t
ranspose and set the column names with the character word extracted-output
Or slightly easier option is to create the
,
at the first space, read withread.csv
, and then set the names with the first column aftert
ransposing