-1

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

subtable1
subtable1
subtable1
subtable2
subtable2
subtable3
subtable3
subtable3
subtable3

我怎样才能在R中做到这一点?

4

3 回答 3

0

假设您正在从文件中读取:

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, ]

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

于 2013-08-06T21:59:05.467 回答
0

在这里我将如何做到这一点。首先,我生成一些数据来模拟问题。

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
于 2013-08-06T22:08:12.233 回答
0

根据我的理解,我将使用 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
于 2013-08-06T23:43:57.637 回答