Finding the index or unique values from a dataframe column

后端 未结 4 800
慢半拍i
慢半拍i 2021-01-22 06:33

I have a dataframe

TableName Function Argument
A         func1    3
B         func1    4
A         func2    6
B         func2    2
C         func1    5
         


        
4条回答
  •  温柔的废话
    2021-01-22 07:11

    I'd suggest to use either simply

    (vec <- tapply(df$Argument, df$TableName, FUN = `identity`))
    # $A
    # [1] 3 6
    #
    # $B
    # [1] 4 2
    #
    # $C
    # [1] 5
    

    or

    (dfNew <- data.frame(TableName = unique(df$TableName), Index = vec))
    #   TableName Index
    # A         A  3, 6
    # B         B  4, 2
    # C         C     5
    

    vec is a list (rather than a character with concatenated numbers) with easy access to names(vec) and, e.g.,

    vec$A
    # [1] 3 6
    

    while dfNew is a data frame whose second column is also a list:

    dfNew[2]
    #   Index
    # A  3, 6
    # B  4, 2
    # C     5
    
    dfNew[,2]
    # [[1]]
    # [1] 3 6
    #
    # [[2]]
    # [1] 4 2
    #
    # [[3]]
    # [1] 5
    
    dfNew[2]["A",][[1]]
    # [1] 3 6
    

    In this case, however, it's not so convenient to reach the indices by TableName, so I'd stick with vec.

提交回复
热议问题