我有一个data.table“result”,它是来自r中arules包的eclat()的输出。
lhs rhs sup conf lift itemset
1: {XXXXXXXXXXX} {XXXXXXXXOOO} 0.001635323 1.0000000 611.50000 1
2: {XXXXXXXXOOO} {XXXXXXXXXXX} 0.001635323 1.0000000 611.50000 1
3: {XXXXX00XXXX} {XXXXXX0XXXX} 0.001635323 1.0000000 32.18421 2
4: {XXXXXXX00XX} {XXX0XXXXXXX} 0.001635323 0.5000000 203.83333 3
5: {XXX0XXXXXXX} {XXXXXXX00XX} 0.001635323 0.6666667 203.83333 3
我想替换“result”的lhs和rhs列中的“{”和“}”。我能够运行gsub并仅使用以下语句返回带有lhs和rhs值的data.table:
result[,lapply(.SD,gsub,pattern = "{",replacement = "",fixed = T),.SDcols=c("lhs","rhs")][,lapply(.SD,gsub,pattern="}",replacement="",fixed=T)]
我得到的输出:
lhs rhs
1: XXXXXXXXXXX XXXXXXXXOOO
2: XXXXXXXXOOO XXXXXXXXXXX
3: XXXXX00XXXX XXXXXX0XXXX
4: XXXXXXX00XX XXX0XXXXXXX
5: XXX0XXXXXXX XXXXXXX00XX
但我也希望保留sup,conf,lift和itemset的值。
预期产出:
lhs rhs sup conf lift itemset
1: XXXXXXXXXXX XXXXXXXXOOO 0.001635323 1.0000000 611.50000 1
2: XXXXXXXXOOO XXXXXXXXXXX 0.001635323 1.0000000 611.50000 1
3: XXXXX00XXXX XXXXXX0XXXX 0.001635323 1.0000000 32.18421 2
4: XXXXXXX00XX XXX0XXXXXXX 0.001635323 0.5000000 203.83333 3
5: XXX0XXXXXXX XXXXXXX00XX 0.001635323 0.6666667 203.83333 3
如何使用其名称获取剩余的列?
答案 0 :(得分:2)
我们可以进行分配以替换我们从lapply
result[,c("lhs", "rhs") := lapply(.SD,gsub,pattern = "[{}]",
replacement = ""), .SDcols=c("lhs","rhs")]
result
# lhs rhs sup conf lift itemset
#1: XXXXXXXXXXX XXXXXXXXOOO 0.001635323 1.0000000 611.50000 1
#2: XXXXXXXXOOO XXXXXXXXXXX 0.001635323 1.0000000 611.50000 1
#3: XXXXX00XXXX XXXXXX0XXXX 0.001635323 1.0000000 32.18421 2
#4: XXXXXXX00XX XXX0XXXXXXX 0.001635323 0.5000000 203.83333 3
#5: XXX0XXXXXXX XXXXXXX00XX 0.001635323 0.6666667 203.83333 3
答案 1 :(得分:1)
library('data.table')
for(col in c("lhs", "rhs")){
set(result, , col, gsub( "[{}]", '', result[[col]] ) ) # using set() function is faster than `[`. see ?`:=`
}
result
# lhs rhs sup conf lift itemset
# 1: XXXXXXXXXXX XXXXXXXXOOO 0.001635323 1.0000000 611.50000 1
# 2: XXXXXXXXOOO XXXXXXXXXXX 0.001635323 1.0000000 611.50000 1
# 3: XXXXX00XXXX XXXXXX0XXXX 0.001635323 1.0000000 32.18421 2
# 4: XXXXXXX00XX XXX0XXXXXXX 0.001635323 0.5000000 203.83333 3
# 5: XXX0XXXXXXX XXXXXXX00XX 0.001635323 0.6666667 203.83333 3