0

I understand pivot_longer but I dont know how to do it chunks, I have entered input & expected output in reproducible dataframe for better understanding

ip= data.frame(
  stringsAsFactors = FALSE,
          Question = c(1L, 2L),
            name_1 = c("abc", "abc2"),
             age_1 = c(12L, 34L),
            name_2 = c("xyz", "xyz2"),
             age_2 = c(34L, 45L)
)

op=data.frame(
  stringsAsFactors = FALSE,
          question = c(1L, 1L, 2L, 2L),
            Member = c(1L, 2L, 1L, 2L),
              Name = c("abc", "xyz", "abc2", "xyz2"),
               age = c(12L, 34L, 34L, 45L)
)

Expected Output

Ideally there are 13 demo inputs for each 20 members, so a programmatic way rather than hard coding would be preferred solution.

Vaibhav Singh
  • 1,159
  • 1
  • 10
  • 25
  • This must be a duplicate; while I'm looking for a suitable link, this is basically `ip %>% pivot_longer(-Question, names_to = c(".value", "Member"), names_sep = "_")`; [update: found a link & closed] – Maurits Evers Oct 30 '20 at 05:54
  • Thanks, I get this error since there are multiple columns, any solution to this Error: Can't combine `eligible_1` and `eligible_3` – Vaibhav Singh Oct 30 '20 at 06:18
  • I can't reproduce; the code snippet I posted works on the sample data you give. The error seems pretty clear though: Data in columns `eligible_1` and `eligible_3` have different types (`numeric` vs. `character`) and can't be combined. This may be an issue with parsing/reading the source data. – Maurits Evers Oct 30 '20 at 06:24
  • No issues, its basic different datatype matching....any solution to different types of datatype... – Vaibhav Singh Oct 30 '20 at 06:26
  • Not sure what you mean; you could convert `eligible_1` to `character` and then reshape. That doesn't make much sense to me though; you should avoid combining data of different types in one column (not tidy). – Maurits Evers Oct 30 '20 at 06:29

0 Answers0