Finding the index or unique values from a dataframe column

后端 未结 4 799
慢半拍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:17

    Here is a dplyr solution where we create a variable with the row_number(), and use that as our index, i.e.

    df %>% 
     mutate(new = row_number()) %>% 
     group_by(TableName) %>% 
     summarise(Index = toString(new))
    

    which gives,

    # A tibble: 3 x 2
      TableName Index
           
    1 A         1, 3 
    2 B         2, 4 
    3 C         5    
    

    You can also save them as lists rather than strings, which will make future operations easier, i.e.

    df %>% 
     mutate(new = row_number()) %>% 
     group_by(TableName) %>% 
     summarise(Index = list(new))
    

    which gives,

    # A tibble: 3 x 2
      TableName Index    
              
    1 A         
    2 B         
    3 C         
    

提交回复
热议问题