我想从另一个表(data.table)创建一个表,该表具有基于条件的附加行。让我们在下表中说,我想在length(indicator)>2
创建一个额外的行。结果应该是下表。
源表如下所示:
id indicator 1 123 abc 2 456 NA 3 456 NA 4 456 NA 5 123 abcd 6 789 abc
dt1 <- data.table(id=c(123, 456, 456, 456, 123, 789), indicator = c("abc", NA, NA, NA, "abcd", "abc"))
结果表应如下所示:
id indicator 1 123 abc 2 123 abc2 3 456 NA 4 456 NA 5 456 NA 6 123 abcd 7 123 abcd2 8 789 abc 9 789 abc2
dt2 <- data.table(id=c(123,123, 456, 456, 456, 123,123,789, 789), indicator = c("abc", "abc2", NA, NA, NA, "abcd", "abcd2", "abc", "abc2"))
答案 0 :(得分:3)
编辑:清洁版礼貌Arun(注意key
创作中添加了data.table
个参数:
dt1 <- data.table(
id=c(123, 456, 456, 456, 123, 789),
indicator = c("abc", NA, NA, NA, "abcd", "abc"),
key=c("id", "indicator")
)
dt1[,
list(indicator=
if(nchar(indicator) > 2)
paste0(indicator, c("", 2:(max(2, .N))))
else
rep(indicator, .N)
),
by=list(indicator, id)
][, -1, with=F]
# id indicator
# 1: 123 abc
# 2: 123 abc2
# 3: 123 abcd
# 4: 123 abcd2
# 5: 456 NA
# 6: 456 NA
# 7: 456 NA
# 8: 789 abc
# 9: 789 abc2
旧版
可能有更优雅的方式,但这样做。基本上,你通过附加数字修饰符(或第一个的“”)来修改那些不符合你条件的行。注意,如果你有非唯一的id /指标,这只会添加另一个数字修饰符(即123-abc,123-abc,最终为123-abc,123-abc2,123-abc3)。
dt1 <- data.table(id=c(123, 456, 456, 456, 123, 789), indicator = c("abc", NA, NA, NA, "abcd", "abc"))
rbind(
dt1[nchar(indicator) <= 2 | is.na(indicator)],
dt1[
nchar(indicator) > 2,
list(indicator=paste0(indicator, c("", 2:(max(2, .N))))),
by=list(indicator, id)
][, -1, with=F]
)[order(id, indicator)]
# id indicator
# 1: 123 abc
# 2: 123 abc2
# 3: 123 abcd
# 4: 123 abcd2
# 5: 456 NA
# 6: 456 NA
# 7: 456 NA
# 8: 789 abc
# 9: 789 abc2