gpt4 book ai didi

r - 如何修改R格式不正确的txt文件?

转载 作者:行者123 更新时间:2023-12-02 04:23:44 24 4
gpt4 key购买 nike

我有几个带有多个数据点的.txt文件,这些文件的头格式不正确,我试图取出不必要的数据,以便R可以读取数据。需要删除一些零件,并需要标识X和Y列。这是文本文件读取内容的示例,其中six指的是X组件,而siy指的是Y组件:

{
"description": "",
"name": "1ml",
"references": [
{
"siclassids": [
],
"siname": "1ml",
"sipoints": [
{
"six": 397.32000732421875,
"siy": 0.8571428656578064
},
{
"six": 400.20001220703125,
"siy": 0.75
},
{
"six": 403.08999633789062,
"siy": 0.60000002384185791

在几个不同的文件中有数百个这些数据点,请问有什么方法可以组织这些数据并读取图中的数据?

谢谢!

最佳答案

您可以使用正则表达式。 grep标识有趣的行。 gsub查找"x""y"及其对应的值,并将它们与,组装在一起。 strsplit在逗号处拆分为一个列表。

l <- readLines("dp.txt")
l <- setNames(do.call(rbind.data.frame,
strsplit(gsub(".+si(.)\\D*(\\d+\\.\\d+).+", "\\1, \\2",
l[grep("\\d{2,}", l)]), ",")), c("axis", "coord"))
l$coord <- as.numeric(l$coord)
l
# axis coord
# 1 x 4
# 2 y 3
# 3 x 5
# 4 y 2
# 5 x 6
# 6 y 1

关于r - 如何修改R格式不正确的txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873188/

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