简体   繁体   中英

insert new line to the top of table r

I've these two tables:

1003 1 0 0 2 0 0 0 0 0 0 0 0 0 
1003 2 0 0 1 0 0 0 0 0 0 0 0 0 
1003 3 2 1 2 2 1 2 1 2 0 0 0 0 
1003 4 2 1 1 2 1 1 1 1 0 0 1 1

and

snp1 1
snp2 2
snp3 3
snp4 4

and I want to add using R the first column of the second table as line in the top of the first table:

snp1 snp2 snp3 snp4
1003 1 0 0 2 0 0 0 0 0 0 0 0 0 
1003 2 0 0 1 0 0 0 0 0 0 0 0 0 
1003 3 2 1 2 2 1 2 1 2 0 0 0 0 
1003 4 2 1 1 2 1 1 1 1 0 0 1 1

Please help, thank you a lot.

Your question is a little vague. Is snp1...snp4 supposed to be some sort of column header? What about the other columns from the first table? What's supposed to be filled in there? I somehow doubt this is really what you want to do, but this does in fact answer your question:

#table 1
x1 <- read.table(text = "1003 1 0 0 2 0 0 0 0 0 0 0 0 0 
1003 2 0 0 1 0 0 0 0 0 0 0 0 0 
1003 3 2 1 2 2 1 2 1 2 0 0 0 0 
1003 4 2 1 1 2 1 1 1 1 0 0 1 1", header = FALSE)

#table 2
x2 <- read.table(text = "snp1 1
snp2 2
snp3 3
snp4 4", header = FALSE)

#insert the first column of table 2 into the first row of table 1
x3 <- rbind(c(as.character(x2[,1]), rep(NA, ncol(x1) - nrow(x2))), x1)

Resulting in:

    V1   V2   V3   V4   V5   V6   V7   V8   V9  V10  V11  V12  V13  V14
1 snp1 snp2 snp3 snp4 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
2 1003    1    0    0    2    0    0    0    0    0    0    0    0    0
3 1003    2    0    0    1    0    0    0    0    0    0    0    0    0
4 1003    3    2    1    2    2    1    2    1    2    0    0    0    0
5 1003    4    2    1    1    2    1    1    1    1    0    0    1    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM