【问题标题】:Add a new column to a combined table to tell the rows are from which sub-table向组合表中添加新列以告知行来自哪个子表
【发布时间】:2013-08-08 02:26:03
【问题描述】:

我有一个由数百个子表组成的组合表,这些子表由 * 分隔。这些子表具有相同的结构,例如 col1 是名称,col2 是重量,col3 是眼睛颜色等。我想删除 * 但在组合表中添加新列以告知子表在哪里表最初来自。新列看起来像

subtable1
subtable1
subtable1
subtable2
subtable2
subtable3
subtable3
subtable3
subtable3

如何在 R 中做到这一点?

【问题讨论】:

  • “表格”是指输入文本/csv 文件吗?
  • 或者你的意思是R中加载的data.frame?
  • 我认为他想将所有这些表读入 R(一些 txt 文件,其中每个表由 ***** 分隔),为每个表添加一列,将其标识为表,然后 @ 987654323@整个事情。

标签: r identity-column


【解决方案1】:

这里我会怎么做。首先我生成一些数据来模拟问题。

text='Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
*******
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
*******
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1'

## read all lines, in your case you give the fileName here
ll <- readLines(textConnection(text))
## detect the sub table delimiter lines
id <- grepl('\\*+',ll)
## removes them from lines and read them using read.table
dat <- read.table(text=ll[!id])
## create the group delimiter using cumsum
dat$table <- paste0('subtable',cumsum(id)[!id])


     V1  V2   V3 V4  V5  V6   V7   V8    V9 V10 V11 V12 V13     table
1  Mazda RX4 21.0  6 160 110 3.90 2.62 16.46   0   1   4   4 subtable0
2 Datsun 710 22.8  4 108  93 3.85 2.32 18.61   1   1   4   1 subtable0
3  Mazda RX4 21.0  6 160 110 3.90 2.62 16.46   0   1   4   4 subtable1
4 Datsun 710 22.8  4 108  93 3.85 2.32 18.61   1   1   4   1 subtable1
5  Mazda RX4 21.0  6 160 110 3.90 2.62 16.46   0   1   4   4 subtable2
6 Datsun 710 22.8  4 108  93 3.85 2.32 18.61   1   1   4   1 subtable2

【讨论】:

    【解决方案2】:

    假设您正在读取文件:

    f <- read.table("filename", fill=TRUE, ....) # insert the required arguments here
    
    # identify separator lines: assume 1st column is '*' and others are all blank
    # tweak specifics to fit
    sep <- f[,1] == "*" & rowSums(!is.na(f[,-1])) == 0
    
    f$subtable <- cumsum(sep) + 1
    f <- f[!sep, ]
    

    这个想法是读入整个文件,然后将分隔线标识为只包含* 的分隔线。由于您没有说明文件的实际内容是什么,因此很难提供更具体的内容。您需要对其进行调整以处理文件包含的任何内容。

    【讨论】:

      【解决方案3】:

      根据我的理解,我将使用 R 中的 mtcars 数据进行说明:

      library(plyr) # for rbind.fill 
      
      # divide the data frames into 2 which is equivalent to 2 sub-tables
      data1<-subset(mtcars,am==0)
      data2<-subset(mtcars,am==1)
      
      # let s be your special sign which is * seperating dataframe 1 and dataframe2 (horizontally)
      data1$s<-rep("*",(dim(data1)[1]))
      
      
      data3<-rbind.fill(data1,data2) # append data1 and data2 
      tablename<-rep(paste0("subtable",1:2),c(dim(data1)[1],dim(data2)[1])) 
      tablename<-as.data.frame(tablename) # generate filename as data frame
      
      mydata<-cbind(data3,tablename) # merge data3 and tablename
      finaldata<-mydata[,-(dim(mydata)[2]-1)] # remove column with seperator which is s
      
      > head(finaldata,n=20)
          mpg cyl  disp  hp drat    wt  qsec vs am gear carb tablename
      1  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 subtable1
      2  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 subtable1
      3  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 subtable1
      4  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 subtable1
      5  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2 subtable1
      6  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2 subtable1
      7  19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4 subtable1
      8  17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4 subtable1
      9  16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3 subtable1
      10 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3 subtable1
      11 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3 subtable1
      12 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4 subtable1
      13 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4 subtable1
      14 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4 subtable1
      15 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 subtable1
      16 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2 subtable1
      17 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2 subtable1
      18 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4 subtable1
      19 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2 subtable1
      20 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 subtable2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 2012-10-28
        • 2016-07-07
        • 1970-01-01
        相关资源
        最近更新 更多