gpt4 book ai didi

R data.table 删除以 "_nd"结尾的列

转载 作者:行者123 更新时间:2023-12-03 15:27:35 25 4
gpt4 key购买 nike

我在 R 中有一个数据表

我想删除以 结尾的列"_nd" .

我的电话号码:



我要



我想要这个脚本的反面

dt[,.SD, .SDcols = patterns("_nd")] 

感谢您的帮助

最佳答案

我会去做的 :

library(data.table)

dt <- data.table(var1 = 1:10, var1_n = 1:10, var2_n = 1:10)
dt
# var1 var1_n var2_n
# <int> <int> <int>
# 1: 1 1 1
# 2: 2 2 2
# 3: 3 3 3
# 4: 4 4 4
# 5: 5 5 5
# 6: 6 6 6
# 7: 7 7 7
# 8: 8 8 8
# 9: 9 9 9
# 10: 10 10 10

to_rm <- grep("_n$", names(dt), value = TRUE)
dt[, (to_rm) := NULL]
dt
# var1
# <int>
# 1: 1
# 2: 2
# 3: 3
# 4: 4
# 5: 5
# 6: 6
# 7: 7
# 8: 8
# 9: 9
# 10: 10

或者更简单:
dt <- data.table(var1 = 1:10, var1_n = 1:10, var2_n = 1:10)
dt[, .SD, .SDcols = grep("_n$", names(dt), invert = TRUE, value = TRUE)]

这也将起作用:
dt <- data.table(var1 = 1:10, var1_n = 1:10, var2_n = 1:10)
to_keep <- grep("_n$", names(dt), invert = TRUE, value = TRUE)
dt[, ..to_keep]

关于R data.table 删除以 "_nd"结尾的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61409698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com