gpt4 book ai didi

r - 处理限制为 2 列的可变空格分隔文件

转载 作者:行者123 更新时间:2023-12-04 12:12:16 25 4
gpt4 key购买 nike

无论出于何种原因,数据都以以下格式提供:

0001 This is text for 0001
0002 This has spaces in between
0003 Yet this is only supposed to be two columns
0009 Why didn't they just comma delimit you may ask?
0010 Or even use quotations?
001 Who knows
0012 But now I'm here with his file
0013 And hoping someone has an elegant solution?

所以上面应该是两列。我想要的是第一个条目的列,即 0001,0002,0003,0009,0010,001,0012,0013以及其他所有内容的另一列。

最佳答案

您可以使用 separate tidyr 包中的函数(将我的评论提升为答案)。您指定两个列名并使用 extra = "merge"参数您确保第一个空格之后的所有内容都放入第二列:

library(tidyr)
separate(mydf, V1, c("nr","text"), sep = " ", extra = "merge")
# or:
mydf %>% separate(V1, c("nr","text"), sep = " ", extra = "merge")

你得到:
    nr                                           text
1 0001 This is text for 0001
2 0002 This has spaces in between
3 0003 Yet this is only supposed to be two columns
4 0009 Why didnt they just comma delimit you may ask?
5 0010 Or even use quotations?
6 001 Who knows
7 0012 But now Im here with his file
8 0013 And hoping someone has an elegant solution?

使用数据:
mydf <- structure(list(V1 = structure(c(1L, 2L, 3L, 4L, 6L, 5L, 7L, 8L), 
.Label = c("0001 This is text for 0001", "0002 This has spaces in between",
"0003 Yet this is only supposed to be two columns", "0009 Why didnt they just comma delimit you may ask?",
"001 Who knows", "0010 Or even use quotations?", "0012 But now Im here with his file", "0013 And hoping someone has an elegant solution?"), class = "factor")),
.Names = "V1", class = "data.frame", row.names = c(NA,-8L))

关于r - 处理限制为 2 列的可变空格分隔文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com