0

I have a data frame with one column as below:

Name
Place
Location
Time
Context
Name
Place
Location
Time
Context

how to I convert the same so that each theme (for eg name) becomes a separate column :

Name Place Location Time Context

Name Place Location Time Context

Just to explain better, I have a column with separate themes with 5 rows and then it repeats.

How to convert the same into multiple columns with each unique five themes?

Thanks

MLavoie
  • 9,671
  • 41
  • 36
  • 56
jalaj pathak
  • 67
  • 1
  • 8
  • hi,Please share some data```dput(df) ``` will be helpful to help. – Tushar Lad Jan 30 '20 at 10:43
  • 1
    Sounds like you want to convert from long to wide format; you can see several approaches [here](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format). And in the future, please include a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – ulfelder Jan 30 '20 at 11:08
  • Hi, i guess my example would help – jalaj pathak Jan 31 '20 at 04:12
  • 1 Place - At the American Bar Association Banking Law Committee Meeting 2020, Washington, D.C. 2 Title - Spontaneity and Order: Transparency, Accountability, and Fairness in Bank Supervision 3 Date - January 17, 2020 4 Speaker - Vice Chair for Supervision Randal K. Quarles – jalaj pathak Jan 31 '20 at 04:13
  • This should come as separate columns, that is one for each, place, title, Date, speaker – jalaj pathak Jan 31 '20 at 04:14

2 Answers2

1

If I create a dataframe with a single column called "variables":

variables <- c("Name", "Place", "Location", "Time", "Context", "Name", "Place" ,"Location","Time", "Context")
my_df <- as.data.frame(variables)

I can get all unique elements of this vector via unique(my_df$variables), and then add them in a loop as an extra column containing NA values (which can later on be replaced)

columns <- as.character(unique(my_df$variables))
for(i in 1:length(columns)){
  my_df[columns[i]] = NA
}


    > my_df
   variables Name Place Location Time Context
1       Name   NA    NA       NA   NA      NA
2      Place   NA    NA       NA   NA      NA
3   Location   NA    NA       NA   NA      NA
4       Time   NA    NA       NA   NA      NA
5    Context   NA    NA       NA   NA      NA
6       Name   NA    NA       NA   NA      NA
7      Place   NA    NA       NA   NA      NA
8   Location   NA    NA       NA   NA      NA
9       Time   NA    NA       NA   NA      NA
10   Context   NA    NA       NA   NA      NA
M M
  • 429
  • 5
  • 13
0

Is this what you are looking for?

# packages
library(tidyr)
library(dplyr)

# create data
name<- c("Anna", "Berta", "Caesar", "Dora")
place <- c("AA", "BB", "CC", "DD")
location <-c("school", "work", "restaurant", "school")
time<-c(08.10, 08.30, 09.00, 10.15)
context<- c("a", "b", "a", "b")

dat<-as.data.frame(cbind(name, place, location, time, context))

dat_long<- dat %>%
  pivot_longer(c(1:5), names_to = "Variable", values_to = "Value")

# backtransform long data frame into wide data frame
dat_wide<- dat_long %>%
  group_by(Variable) %>%  # group by everything other than the value column 
  mutate(row_id=1:n()) %>% ungroup() %>%  # build group index
  pivot_wider(names_from = "Variable", values_from = "Value") %>%  # spread
  select(-row_id) 
Em Laskey
  • 508
  • 4
  • 15