dd <- read.table(header = TRUE, text = 'population X354045 X430045 X995019
Crater NA 11 22
Teton 11 31 11')
nr <- nrow(dd)
dd <- dd[rep(1:2, each = nr), ]
# population X354045 X430045 X995019
# 1 Crater NA 11 22
# 1.1 Crater NA 11 22
# 2 Teton 11 31 11
# 2.1 Teton 11 31 11
dd[, -1] <- lapply(dd[, -1], function(x) {
idx <- (seq_along(x) %% 2 == 0) + 1L
substr(x, idx, idx)
})
# population X354045 X430045 X995019
# 1 Crater <NA> 1 2
# 1.1 Crater <NA> 1 2
# 2 Teton 1 3 1
# 2.1 Teton 1 1 1
或者只是
dd <- dd[rep(1:2, each = nr), ]
dd[, -1] <- lapply(dd[, -1], function(x)
Vectorize(substr)(x, rep(1:2, nr), rep(1:2, nr)))
會工作
而且在data.table
感謝@DavidArenburg
library('data.table')
dd <- read.table(header = TRUE, text = 'population X354045 X430045 X995019
Crater NA 11 22
Teton 11 31 11')
setDT(dd)[rep(1:2, each = .N), lapply(.SD, substr, 1:2, 1:2), by = population]
# population X354045 X430045 X995019
# 1: Crater NA 1 2
# 2: Crater NA 1 2
# 3: Teton 1 3 1
# 4: Teton 1 1 1
或類似的,同樣的想法,但避免了by
部分
dd <- setDT(dd)[rep(1:2, each = .N)]
dd[, 2:4 := dd[ ,lapply(.SD, substr, 1:2, 1:2), .SD = -1]]
,如果你是一個大數據的工作集
感謝@DavidArenburg更新,現在大家一定會想到我使用數據表! – rawr
你明確表示這是我的一個好主意 –