create a dataset of some repeated id numbers of different frequencies in one column and corresponding values, then group by the id and change values into separate columns in r

To create a dataset with repeated id numbers and corresponding values, and then group by the id and change values into separate columns in R, you can use the following code:

main.r
# Load necessary libraries
library(dplyr)
library(tidyr)

# Create a dataset with repeated id numbers and corresponding values
set.seed(123)
id <- c(1, 1, 1, 2, 2, 3, 3, 3, 3)
value <- c("a", "b", "c", "d", "e", "f", "g", "h", "i")
df <- data.frame(id, value)

# Print the original dataset
print(df)

# Group by id and change values into separate columns
df_wide <- df %>% 
  group_by(id) %>% 
  mutate(row = row_number()) %>% 
  pivot_wider(names_from = row, values_from = value)

# Print the transformed dataset
print(df_wide)
529 chars
22 lines

This will create a dataset with repeated id numbers and corresponding values, and then group by the id and change values into separate columns.

Example output:

main.r
# Original dataset
  id value
1  1     a
2  1     b
3  1     c
4  2     d
5  2     e
6  3     f
7  3     g
8  3     h
9  3     i

# Transformed dataset
# A tibble: 3 x 4
     id `1`   `2`   `3`  
  <dbl> <chr> <chr> <chr>
1     1 a     b     c     
2     2 d     e     NA    
3     3 f     g     h     
303 chars
20 lines

Note that if there are different numbers of values for each id, the resulting columns will be filled with NA for the ids with fewer values.

If you want to avoid this and only keep the common number of values across all ids, you can use the following code instead:

main.r
# Group by id and change values into separate columns
max_n <- max(table(df$id))
df_wide <- df %>% 
  group_by(id) %>% 
  mutate(row = row_number()) %>% 
  pivot_wider(names_from = row, values_from = value)

# Remove rows with more values than the common number
df_wide <- df_wide[, 1:(max_n + 1)]
298 chars
10 lines

gistlibby LogSnag