Truncating a dataframe according to count of vector elements

前端 未结 2 1812
小鲜肉
小鲜肉 2021-01-25 08:27

I have a dataframe df, containing three vectors:

subject  condition  value
01       A          12
01       A          6
01       B          10
01            


        
2条回答
  •  时光说笑
    2021-01-25 08:58

    Here's a dplyr solution -

    df %>%
      group_by(subject, condition) %>%
      filter(row_number() < 3) %>%
      ungroup()
    
    # A tibble: 8 x 3
      subject condition value
              
    1 01      A            12
    2 01      A             6
    3 01      B            10
    4 01      B             2
    5 02      A             5
    6 02      A            11
    7 02      B             3
    8 02      B             5
    

提交回复
热议问题