w*******n 发帖数: 469 | 1 Given the data like:
ID comp1 Num1 comp2 Num2 comp3 Num3
1 A 2 A 2 D NA
2 B 2 C NA F NA
3 C 2 E NA G NA
4 A 2 D NA H NA
5 B 1 K 1 A 1
6 C 1 B 1 C 1
7 A 2 D 2 B NA
8 B 2 C NA A NA
Want a data like:
ID comp Num
1 A 2
2 B 2
3 C 2
4 A 2
5 B 1
6 C 1
7 A 2
8 B 2
1 A 2
2 C NA
3 E NA
4 D NA
5 K 1
6 B 1
7 D 2
8 C NA
1 D NA
2 F NA
3 G NA
4 H NA
5 A 1
6 C 1
7 B NA
8 A NA | Z**0 发帖数: 1119 | 2 不就是把数据读入为data frame,然后用rbindlist把数据bind一下?
df <- read.csv('data.txt', sep=' ')
d1 <- df[,c("ID","comp1","Num1")]
d2 <- df[,c("ID","comp1","Num2")]
d3 <- df[,c("ID","comp3","Num3")]
result <- rbindlist(list(d1, d2, d3))
colnames(result) <- c("ID","comp","Num")
如果直接用rbind,需要把每个df的col names统一。 | w*******n 发帖数: 469 | 3 Thanks.
I would like use some function in R like reshape, melt or cast/decast,
supposing I have quite a lot of columns like this.
Or is there any way to do the rbind but ignoring the name difference?
【在 Z**0 的大作中提到】 : 不就是把数据读入为data frame,然后用rbindlist把数据bind一下? : df <- read.csv('data.txt', sep=' ') : d1 <- df[,c("ID","comp1","Num1")] : d2 <- df[,c("ID","comp1","Num2")] : d3 <- df[,c("ID","comp3","Num3")] : result <- rbindlist(list(d1, d2, d3)) : colnames(result) <- c("ID","comp","Num") : 如果直接用rbind,需要把每个df的col names统一。
| Z**0 发帖数: 1119 | 4 Here you go.
df <- read.csv('data.txt', sep=' ')
l <- reshape(df, idvar="ID", varying = list(c("comp1","comp2","comp3"),c("
Num1","
Num2","Num3")), v.names = c("comp", "Num"), direction="long")
【在 w*******n 的大作中提到】 : Thanks. : I would like use some function in R like reshape, melt or cast/decast, : supposing I have quite a lot of columns like this. : Or is there any way to do the rbind but ignoring the name difference?
| n*****3 发帖数: 1584 | 5 you can use melt for the reshape package.
【在 Z**0 的大作中提到】 : Here you go. : df <- read.csv('data.txt', sep=' ') : l <- reshape(df, idvar="ID", varying = list(c("comp1","comp2","comp3"),c(" : Num1"," : Num2","Num3")), v.names = c("comp", "Num"), direction="long")
| c***3 发帖数: 280 | 6 Zer0 解法的简化版(用column number代替column name):
l <- reshape(df, direction="long", idvar=1,
varying=c(c(2,4,6), c(3,5,7)), sep="") |
|