【问题标题】:Issue with getting column names获取列名的问题
【发布时间】:2018-04-11 20:34:31
【问题描述】:

我有一张如下表。(Table1)

Table1:

Type   Fund   Con_counts
5510   COM    1
5520   COM    2
0300   COM    2
5510   COM    1
.

并使用以下内容

Table1 <- Table1[, list(Con_counts = sum(as.double(Con_counts), na.rm = TRUE)), by = list(Type, Fund)] 

如何使用代码获取由逗号 (,) 分隔的列名,以便可以放在 () 括号内。

例如,我尝试了下面的代码,但不起作用。

columns <- colnames(Table2)[!(names(Table2) %in% c("Con_counts"))]

Table1[, list(Con_counts = sum(as.double(Con_counts), na.rm = TRUE)), by = list(columns)]

列应该是Type,Fund

Output: 
    Type   Fund   Con_counts
    5510   COM    2
    5520   COM    2
    0300   COM    2

【问题讨论】:

  • 你试过by=columns而不用list(...)包装吗?

标签: r list data.table names


【解决方案1】:

我确信以前有人问过这个答案,但我没有找到好的重复。

这就是我在data.table 语法中使用setdiff() 函数要做的事情:

columns <- setdiff(names(Table1), c("Con_counts"))
Table1[, .(Con_counts = sum(as.double(Con_counts), na.rm = TRUE)), by = columns] 
   Type Fund Con_counts
1: 5510  COM          2
2: 5520  COM          2
3:  300  COM          2

数据

library(data.table)
Table1 <- fread(
"  Type   Fund   Con_counts
5510   COM    1
5520   COM    2
0300   COM    2
5510   COM    1")

【讨论】:

    【解决方案2】:

    这是dplyr中的一个简单解决方案:

    library(dplyr)
    Table1 <- data.frame(Type = c(5510,5520,0300,5510),
                         Fund = c('COM','COM','COM','COM'),
                         Con_counts = c(1,2,2,1))
    
    Table1 %>%
         group_by(Type,Fund) %>%
         summarise(Con_counts = sum(Con_counts)) %>%
    
       Type Fund  Con_counts
      <dbl> <fct>      <dbl>
    1  300. COM           2.
    2 5510. COM           2.
    3 5520. COM           2.
    

    如果您仍想使用data.tables,我无法为您提供帮助,但下面的答案可以帮助您获取列名:

    要获取Table1 的列名,只需使用返回字符向量的colnames(Table1)

    columns <- colnames(Table1)
    columns
    [1] 'Type'   'Fund'   'Con_counts'
    

    如果您想从该列表中删除 'Con_counts,有几个选项:

    # If you know that 'Con_counts' is the 3rd column,
    #   you can call colnames on a sliced version of the data frame
    colnames(Table1[,-3])
    [1] 'Type'   'Fund'
    
    colnames(Table1[,c(-2,-3)])
    [1] 'Type'
    
    
    # If you want to drop by name
    columns <- colnames(Table1)
    columns[columns != 'Con_counts']
    [1] 'Type'   'Fund'
    
    # If you want to drop by multiple names
    columns[!(columns %in% c('Con_counts', 'Type'))]
    [1] 'Fund'
    

    您可以通过将该变量插入括号表示法来选择这些列。

    Table[1, columns]
    # Type   Fund   Con_counts
    # 5510   COM    1
    

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2010-12-04
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多