在文本文件中选择特定行和单元格并放入数据框的R代码已断开
这是一个扩展名,需要对其进行修改,以适应文本文件中更多的行在文本文件中选择特定行和单元格并放入数据框的R代码已断开,r,dataframe,filter,R,Dataframe,Filter,这是一个扩展名,需要对其进行修改,以适应文本文件中更多的行带区。我想要的是从一个文本文件中选择“基本统计”行,它看起来像下面的一个,然后将它们组织在一个数据框中,就像问题底部的那个。如果要直接使用该文件,请提供指向该文件的链接 Filename: /blah/blah/blah.txt ROI: red_2 [Red] 12 points Basic Stats Min Max Mean Stdev Band 1 0.032262
带区。我想要的是从一个文本文件中选择“基本统计”行,它看起来像下面的一个,然后将它们组织在一个数据框中,就像问题底部的那个。如果要直接使用该文件,请提供指向该文件的链接
Filename: /blah/blah/blah.txt
ROI: red_2 [Red] 12 points
Basic Stats Min Max Mean Stdev
Band 1 0.032262 0.124425 0.078073 0.028031
Band 2 0.021072 0.064156 0.037923 0.012178
Band 3 0.013404 0.066043 0.036316 0.014787
Band 4 0.005162 0.055781 0.015526 0.013255
Histogram DN Npts Total Percent Acc Pct
Band 1 0.032262 1 1 8.3333 8.3333
Bin=0.00036 0.032624 0 1 0.0000 8.3333
0.032985 0 1 0.0000 8.3333
0.033346 0 1 0.0000 8.3333
这是我正在使用的代码:
dat <- readLines('/blah/blah/blah.txt')
# create an index for the lines that are needed: Basic stats and Bands
ti <- rep(which(grepl('ROI:', dat)), each = 8) + 1:8
# create a grouping vector of the same length
grp <- rep(1:203, each = 8)
# filter the text with the index 'ti'
# and split into a list with grouping variable 'grp'
lst <- split(dat[ti], grp)
# loop over the list a read the text parts in as dataframes
lst <- lapply(lst, function(x) read.table(text = x, sep = '\t', header = TRUE, blank.lines.skip = TRUE))
# bind the dataframes in the list together in one data.frame
DF <- do.call(rbind, lst)
# change the name of the first column
names(DF)[1] <- 'ROI'
# get the correct ROI's for the ROI-column
DF$ROI <- sub('.*: (\\w+).*$', '\\1', dat[grepl('ROI: ', dat)])
DF
我需要一些帮助。对于此文件,您必须进行调整。对于链接文本文件(test2.txt
),我提出以下方法:
dat <- readLines('test2.txt')
len <- sum(grepl('ROI:', dat))
ti <- rep(which(grepl('ROI:', dat)), each = 7) + 0:6
grp <- rep(1:len, each = 7)
lst <- split(dat[ti], grp)
lst <- lapply(lst, function(x) read.table(text = x, sep = '\t', skip = 1, header = TRUE, blank.lines.skip = TRUE))
names(lst) <- sub('.*: (\\w+).*$', '\\1', dat[grepl('ROI: ', dat)])
library(data.table)
DT <- rbindlist(lst, idcol = 'ROI')
setnames(DT, 2, 'Band')
我建议首先使用bash(unix工具)整理数据,然后加载到R(例如bands.txt…
)。请提供dput(dat)或子集,以便我们可以复制和粘贴。太棒了!非常优雅。你能解释一下+0.6
在ti
行中的意思吗?谢谢@Jaap@JAG2024这条线的作用是创建需要读取的线向量。例如,检查5+0:2
ROI Band Min Max Mean Stdev
red_2 Band 1 0.032262 0.124425 0.078073 0.028031
red_2 Band 2 0.021072 0.064156 0.037923 0.012178
red_2 Band 3 0.013404 0.066043 0.036316 0.014787
red_2 Band 4 0.005162 0.055781 0.015526 0.013255
red_3 Band 1 values...
red_4 Band 2
red_4 Band 3
red_4 Band 4
dat <- readLines('test2.txt')
len <- sum(grepl('ROI:', dat))
ti <- rep(which(grepl('ROI:', dat)), each = 7) + 0:6
grp <- rep(1:len, each = 7)
lst <- split(dat[ti], grp)
lst <- lapply(lst, function(x) read.table(text = x, sep = '\t', skip = 1, header = TRUE, blank.lines.skip = TRUE))
names(lst) <- sub('.*: (\\w+).*$', '\\1', dat[grepl('ROI: ', dat)])
library(data.table)
DT <- rbindlist(lst, idcol = 'ROI')
setnames(DT, 2, 'Band')
> DT
ROI Band Min Max Mean Stdev
1: red_1 Band 1 0.013282 0.133982 0.061581 0.034069
2: red_1 Band 2 0.009866 0.112935 0.042688 0.026618
3: red_1 Band 3 0.008304 0.037059 0.018434 0.007515
4: red_1 Band 4 0.004726 0.040089 0.018490 0.009605
5: red_2 Band 1 0.032262 0.124425 0.078073 0.028031
---
1220: bcs_49 Band 4 0.002578 0.010578 0.006191 0.002285
1221: bcs_50 Band 1 0.032775 0.072881 0.051152 0.012593
1222: bcs_50 Band 2 0.020029 0.085993 0.042864 0.018628
1223: bcs_50 Band 3 0.012770 0.034367 0.023056 0.006581
1224: bcs_50 Band 4 0.005804 0.024798 0.014049 0.005744