如何通过R中的索引从数据表中提取列?(How to extract columns from data table by indices in R?)
我想从名为dt的数据表中提取第4,第5和第6列
以下方法有效:
dt[, c(4,5,6)]但以下不是:
a = c(4,5,6) dt[, a]事实上,第二种方法给了我一个结果:
4 5 6有人能告诉我为什么会这样吗? 这两种方法看起来与我相同。
I want to extract the 4th, 5th, and 6th column from a data table named dt
the following method works:
dt[, c(4,5,6)]but the following doesn't:
a = c(4,5,6) dt[, a]In fact, the second method gives me a reult of:
4 5 6Can someone tell me why this is happening? The two method looks equivalent to me.
最满意答案
我们可以在对象'a'之前使用双点( .. )来提取列
dt[, ..a] # col4 col5 col6 #1: 4 5 6 #2: 5 6 7 #3: 6 7 8 #4: 7 8 9或者另一个选项是with = FALSE
dt[, a, with = FALSE]数据
dt <- data.table(col1 = 1:4, col2 = 2:5, col3 = 3:6, col4 = 4:7, col5 = 5:8, col6 = 6:9)We can use double dots (..) before the object 'a' to extract the columns
dt[, ..a] # col4 col5 col6 #1: 4 5 6 #2: 5 6 7 #3: 6 7 8 #4: 7 8 9Or another option is with = FALSE
dt[, a, with = FALSE]data
dt <- data.table(col1 = 1:4, col2 = 2:5, col3 = 3:6, col4 = 4:7, col5 = 5:8, col6 = 6:9)更多推荐
发布评论