Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe manipulation in R - Assign a value to nth row based on row values numbers

Tags:

r

dplyr

I have this example dataframe "df":

id <- c(1001, 1002)
col2 <- c(5, 2)
col3 <- c(1, 4)
df <- data.frame(id, col2, col3)

Is there an easy way to convert this data frame so that the new data frame contains same column names but assigns "1" to the nth row that corresponds to each value and assigns "0" to the remaining slots? It seemed doable but somewhat difficult. The resulting table will be as follows (df_results):

id <- c(rep(1001, 5), rep(1002, 5))
col2 <- c(0,0,0,0,1, 0,1,0,0,0)
col3 <- c(1,0,0,0,0,0,0,0,1,0)
df_results <- data.frame(id, col2, col3)
like image 717
Zipsa Avatar asked Oct 26 '25 15:10

Zipsa


1 Answers

You can uncount() using the parallel max of your cols, then grouping by id, check if the value equals the row number:

library(dplyr)
library(tidyr)

df %>%
  uncount(pmax(col2, col3)) %>%
  group_by(id) %>%
  mutate(across(starts_with("col"), ~ as.numeric(.x == row_number()))) %>%
  ungroup()

# A tibble: 9 × 3
     id  col2  col3
  <dbl> <dbl> <dbl>
1  1001     0     1
2  1001     0     0
3  1001     0     0
4  1001     0     0
5  1001     1     0
6  1002     0     0
7  1002     1     0
8  1002     0     0
9  1002     0     1
like image 56
Ritchie Sacramento Avatar answered Oct 29 '25 04:10

Ritchie Sacramento