Move columns into rows

Hi!

I want to move columns into rows. There are clients "firts go" variables and "revisit" variables in data. I want to move "revisit" under "first go" into rows. Sorry messy writing, I try to clear this thing...

Data now (four variables):
clients first go date - clients first go place - clients revisit date - clients revisit place
1.1.2020 - Stockholm - 12.3.2020 - Göteborg

How I want it (two variables):
clients first go date - clients first go place
1.1.2020 - Stockholm
12.3.2020 - Göteborg

I have 36 variables in my data and after this hopeful only 13 :slight_smile:

  • Sami
library(tidyverse)

(mydata <- data.frame(
  cd1=c(1,3),
  cp1=c("a","c"),
  cd2=c(2,4),
  cp2=c("b","d")
))

(first <- mydata %>% select(date=cd1,place=cp1) %>% mutate(rn=row_number()))
(second <- mydata %>% select(date=cd2,place=cp2)%>% mutate(rn=row_number()))

(fin <- bind_rows(first,second) %>% arrange(rn))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.