假設你的輸入是dataframes,那麼你可以做到以下幾點:
library(data.table)
setDT(ma)[, lapply(.SD, function(x) x = unlist(df[match(x,df$ID),"1990"])),
.SDcols=colnames(ma)]
返回:
a b c
1: 10 13 16
2: 19 22 25
3: 28 31 34
說明:
- 隨着
setDT(ma)
你轉變將數據幀轉換爲數據表(其是增強型數據幀)。
- 使用
.SDcols=colnames(ma)
您可以指定要應用轉換的列。
lapply(.SD, function(x) x = unlist(df[match(x,df$ID),"1990"]))
對.SDcols
指定的每列執行匹配操作。
與data.table
的另一種方法是首先轉化ma
到長data.table:
ma2 <- melt(setDT(ma), measure.vars = c("a","b","c"))
setkey(ma2, value) # set key by which 'ma' has to be indexed
setDT(df, key="ID") # transform to a datatable & set key by which 'df' has to be indexed
# joining the values of the 1990 column of df into
# the right place in the value column of 'ma'
ma2[df, value := `1990`]
其給出:
> ma2
variable value
1: a 10
2: b 13
3: c 16
4: a 19
5: b 22
6: c 25
7: a 28
8: b 31
9: c 34
這個方法的唯一缺點是'值'列中的數值得到stor編輯爲字符值。您可以通過擴展它如下更正此:
ma2[df, value := `1990`][, value := as.numeric(value)]
如果你想改回寬幅你可以使用從development version of data.table
(v1.9.7+)新rowid
功能:
ma3 <- dcast(ma2, rowid(variable)~variable, value.var = "value")[, variable:=NULL]
這給:
> ma3
a b c
1: 10 13 16
2: 19 22 25
3: 28 31 34
使用的數據
ma <- structure(list(a = structure(1:3, .Label = c("ID.aa", "ID.ba", "ID.ca"), class = "factor"),
b = structure(1:3, .Label = c("ID.ab", "ID.bb", "ID.cb"), class = "factor"),
c = structure(1:3, .Label = c("ID.ac", "ID.bc", "ID.cc"), class = "factor")),
.Names = c("a", "b", "c"), class = "data.frame", row.names = c(NA, -3L))
df <- structure(list(ID = structure(1:9, .Label = c("ID.aa", "ID.ab", "ID.ac", "ID.ba", "ID.bb", "ID.bc", "ID.ca", "ID.cb", "ID.cc"), class = "factor"),
`1990` = c(10L, 13L, 16L, 19L, 22L, 25L, 28L, 31L, 34L),
`1991` = c(11L, 14L, 17L, 20L, 23L, 26L, 29L, 32L, 35L),
`1992` = c(12L, 15L, 18L, 21L, 24L, 27L, 30L, 33L, 36L)),
.Names = c("ID", "1990", "1991", "1992"), class = "data.frame", row.names = c(NA, -9L))
不應在第2行中的'預期輸出的了'柱'19'而不是'25'? – Jaap
是的,絕對。謝謝你指出這個錯字! –