如何在 R 中保留文本文件 (.excel) 的行和列格式

How to retain row and column formate of a text file(.cel) in R

我在 R 中读取了一个文本文件,如下所示,有 1354896 行和 5 列。

我尝试read.table()read.delim()上传文件,但是上传后的文件格式发生了变化。它将所有内容转换为一列。

OffsetY=0
GridCornerUL=258 182
GridCornerUR=8450 210
GridCornerLR=8419 8443
GridCornerLL=228 8414
Axis-invertX=0
AxisInvertY=0
swapXY=0
DatHeader=[19..65528]  PA-D 102 Full:CLS=8652 RWS=8652 XIN=1  YIN=1  VE=30        2.0 11/04/03 12:49:30 50205710  M10      HG-U133_Plus_2.1sq                  6
Algorithm=Percentile
AlgorithmParameters=Percentile:75;CellMargin:2;OutlierHigh:1.500;OutlierLow:1.004;AlgVersion:6.0;FixedCellSize:TRUE;FullFeatureWidth:7;FullFeatureHeight:7;IgnoreOutliersInShiftRows:FALSE;FeatureExtraction:TRUE;PoolWidthExtenstion:2;PoolHeightExtension:2;UseSubgrids:FALSE;RandomizePixels:FALSE;ErrorBasis:StdvMean;StdMult:1.000000

[INTENSITY]
NumberCells=1354896
CellHeader=X    Y   MEAN    STDV    NPIXELS
  0   0 147.0   23.5     25
  1   0 10015.0 1276.7   25
  2   0 160.0   24.7     25
  3   0 9710.0  1159.8   25
  4   0 85.0    14.0     25
  5   0 171.0   21.0     25
  6   0 11648.0 1678.4   25
  7   0 163.0   30.7     25
  8   0 12044.0 1430.1   25
  9   0 169.0   25.7     25
 10   0 11646.0 1925.6   25
 11   0 176.0   30.7     25

阅读后格式更改如下:

  1. 我想保留行和列的格式
  2. 我想删除 [intensity] 之前的所有内容,如第一个文件中显示的 (offset, GridCornerUL, so on)

你可以试试:

txt <- readLines("file.txt")
df <- read.csv(text = txt[-(1:grep("NumberCells=\d+", txt))], check.names = FALSE)
write.csv(df, tf <- tempfile(fileext = ".csv"), row.names = FALSE)

read.csv(tf, check.names = FALSE) # just to verify...
#    CellHeader=X    Y   MEAN    STDV    NPIXELS
# 1                    0   0 147.0   23.5     25
# 2                    1   0 10015.0 1276.7   25
# 3                    2   0 160.0   24.7     25
# 4                    3   0 9710.0  1159.8   25
# 5                    4   0 85.0    14.0     25
# 6                    5   0 171.0   21.0     25
# 7                    6   0 11648.0 1678.4   25
# 8                    7   0 163.0   30.7     25
# 9                    8   0 12044.0 1430.1   25
# 10                   9   0 169.0   25.7     25
# 11                  10   0 11646.0 1925.6   25
# 12                  11   0 176.0   30.7     25

这会省略 NumberCells=1354896 之前的所有内容,包括 NumberCells=1354896

如果 NumberCells= 总是出现在 header 行之前,那么你可以利用它来告诉你要跳过的行数:

dat<-readLines("file.txt")
read.table(textConnection(dat), header=TRUE, skip=grep("NumberCells", dat))
#   CellHeader.X Y  MEAN   STDV NPIXELS
#1             0 0   147   23.5      25
#2             1 0 10015 1276.7      25
#3             2 0   160   24.7      25
#4             3 0  9710 1159.8      25
#5             4 0    85   14.0      25
#6             5 0   171   21.0      25
#7             6 0 11648 1678.4      25
#8             7 0   163   30.7      25
#9             8 0 12044 1430.1      25
#10            9 0   169   25.7      25
#11           10 0 11646 1925.6      25
#12           11 0   176   30.7      25

编辑

因为您的文件有很多行,您可能希望限制 readLines 读入的行数。为此,您需要知道 [=20] 之前的最大行数=]行。例如,如果您知道 header 行将始终位于文件的前 200 行内,​​您可以执行以下操作:

dat<-readLines("file.txt", n=200)
read.table("file.txt", header=TRUE, skip=grep("NumberCells", dat))

当您使用 linux 时,另一种选择是 pipe awkread.tablefread

read.table(pipe("awk 'NR==1, /NumberCells/ {next}{print}' Hashim.txt"),
      header=TRUE, check.names=FALSE)
#    CellHeader=X Y  MEAN   STDV NPIXELS
#1             0 0   147   23.5      25
#2             1 0 10015 1276.7      25
#3             2 0   160   24.7      25
#4             3 0  9710 1159.8      25
#5             4 0    85   14.0      25
#6             5 0   171   21.0      25
#7             6 0 11648 1678.4      25
#8             7 0   163   30.7      25
#9             8 0 12044 1430.1      25
#10            9 0   169   25.7      25
#11           10 0 11646 1925.6      25
#12           11 0   176   30.7      25