Home > Blockchain >  Shaping a data and affecting the count column in R
Shaping a data and affecting the count column in R

Time:09-16

I have a data like this :

df<-structure(list(concept = c("agree", "anger", "anxiety", "cognitive", 
"cognitive"), count = c(1L, 2L, 4L, 6L, 122L)), class = "data.frame", row.names = c(NA, 
-5L))

I need to have new columns for each value of the "concept" column that get the number in the "count" column and the desire data is :

new_df<-structure(list(agree = c(1L, 0L, 0L, 0L, 0L), anger = c(0L, 2L, 
0L, 0L, 0L), anxiety = c(0L, 0L, 4L, 0L, 0L), cognitive = c(0L, 
0L, 0L, 6L, 122L)), class = "data.frame", row.names = c(NA, -5L
))

  agree anger anxiety cognitive
1     1     0       0         0
2     0     2       0         0
3     0     0       4         0
4     0     0       0         6
5     0     0       0       122

CodePudding user response:

We could use pivot_wider

library(dplyr)
library(tidyr)
 df %>% 
  mutate(rn = row_number()) %>% 
 pivot_wider(names_from = concept, values_from =count, 
  values_fill = 0) %>% 
 select(-rn)

-output

# A tibble: 5 × 4
  agree anger anxiety cognitive
  <int> <int>   <int>     <int>
1     1     0       0         0
2     0     2       0         0
3     0     0       4         0
4     0     0       0         6
5     0     0       0       122

Or use xtabs from base R

xtabs(count ~ rn   concept, transform(df, rn = seq_along(concept)))
concept
rn  agree anger anxiety cognitive
  1     1     0       0         0
  2     0     2       0         0
  3     0     0       4         0
  4     0     0       0         6
  5     0     0       0       122

CodePudding user response:

another base R solution:

as.data.frame.matrix(xtabs(count~., cbind(seq(nrow(df)), df)))

  agree anger anxiety cognitive
1     1     0       0         0
2     0     2       0         0
3     0     0       4         0
4     0     0       0         6
5     0     0       0       122
  • Related