破解R代码以选择文本文件中的特定行和单元格并放入数据框中

时间:2017-03-05 22:11:01

标签: r dataframe filter

这是this question的扩展,需要对其进行更改以在文本文件中容纳更多行loc。我想从文本文件中选择看起来像下面那一行的“基本属性”行,然后在数据框中组织它们,就像问题底部的那样。如果您想直接使用该文件,请Here's指向该文件的链接。

df.loc[slc.index[0]] = 0
print(df)

   A
0  1
1  2
2  0
3  4
4  5

这是我正在使用的代码:

Bands

输出看起来像这样:

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

我想要一些帮助。

1 个答案:

答案 0 :(得分:2)

对于此文件,您必须调整the approach I proposed here。对于链接的文本文件(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')

给出了期望的结果:

> 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