[英]How to create data.table with additional rows from table based on a condition
I would like to create a table out of another table (data.table) that has additional rows based on a condition.我想从另一个表 (data.table) 中创建一个表,该表具有基于条件的附加行。 Lets say in the following table, I want to create an additional row if length(indicator)>2
.让我们在下表中说,如果length(indicator)>2
,我想创建一个额外的行。 The result should be the table below.结果应该是下表。
The source table looks like this:源表如下所示:
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"))
Resulting table should look like this:结果表应如下所示:
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"))
EDIT: cleaner version courtesy Arun (note there is a key
argument added to the data.table
creation):编辑:由 Arun 提供的更干净的版本(注意在data.table
创建中添加了一个key
参数):
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]
# 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
Old version旧版本
There probably is a more elegant way, but this will do it.可能有一种更优雅的方式,但这会做到。 Basically, you rbind the rows that don't meet your condition, with those that do, modified by appending the numeric modifier (or "" for the first one).基本上,您将不符合条件的行与符合条件的行进行绑定,通过附加数字修饰符(或“”作为第一个修饰符)进行修改。 Note, if you have non-unique id/indicators, this will just add another numeric modifier (ie 123-abc, 123-abc, ends up as 123-abc, 123-abc2, 123-abc3).请注意,如果您有非唯一的 id/indicators,这只会添加另一个数字修饰符(即 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]
)[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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.