Home > Blockchain >  R Function to Create New Column based on Counting Value
R Function to Create New Column based on Counting Value

Time:10-23

I want to ask, how to create new column based on the occurence of a certain value in a certain column

This is the example :

I have this DF:

example_twins
#> # A tibble: 4 × 2
#>   family name      
#>   <chr>  <chr> 
#> 1 Kelly  Mark      
#> 2 Kelly  Scott     
#> 3 Quin   Tegan     
#> 4 Quin   Sara

I wanted to count family name into new Order in new Column (n)

    example_twins
#> # A tibble: 4 × 3
#>   family name      n
#>   <chr>  <chr> <dbl>
#> 1 Kelly  Mark      1
#> 2 Kelly  Scott     2
#> 3 Quin   Tegan     1
#> 4 Quin   Sara      2
    

Thank You!

CodePudding user response:

With dplyr...


library(dplyr, warn = FALSE)

twins_df |> 
  group_by(family) |> 
  mutate(n = row_number())

#> # A tibble: 4 × 3
#> # Groups:   family [2]
#>   family name      n
#>   <chr>  <chr> <int>
#> 1 Kelly  Mark      1
#> 2 Kelly  Scott     2
#> 3 Quin   Tegan     1
#> 4 Quin   Sara      2

data

twins_df <- data.frame(family = c("Kelly", "Kelly", "Quin", "Quin"),
                       name = c("Mark", "Scott", "Tegan", "Sara"))

Created on 2022-10-23 with reprex v2.0.2

CodePudding user response:

data.table option with grouping on family like this:

example_twins <- read.table(text = 'family name
Kelly  Mark      
Kelly  Scott     
Quin   Tegan     
Quin   Sara', header = TRUE)

library(data.table)
setDT(example_twins)
example_twins[, n := seq(.N), by = family][]
#>    family  name n
#> 1:  Kelly  Mark 1
#> 2:  Kelly Scott 2
#> 3:   Quin Tegan 1
#> 4:   Quin  Sara 2

Created on 2022-10-23 with reprex v2.0.2

  • Related