-2

I am working with a dataset, which I firstly converted from long to wide, because I need the rows (variables) to be columns:

I used:

library(tidyr)
cfa_model<-pivot_wider(cfa_data, names_from= variable, values_from = value)

and got:

I need this data to be as a dataframe, having 48 rows and 65 columns, not every column has the same length and I don't know if this is a problem, for the case when I got less observations a NA would be just fine. The problem when covnerting to dataframe is that I got the values as a list all 48 rows in 1, I need each column to be as a normal data frame numeric variable.

Do you guys know how to fix this?

Thanks a lot! :)

Matt
  • 7,255
  • 2
  • 12
  • 34
  • Provide a reproducible example, refer https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nad Pat Oct 12 '21 at 08:57
  • Use `dput` to provide the data rather than images as these cannot be copied for us to work on the problem. – Tjn25 Oct 12 '21 at 08:59

1 Answers1

0

If you want to use pivot_wider, one approach is to add row numbers for each variable to make unique first:

library(tidyverse)

cfa_data %>%
  group_by(variable) %>%
  mutate(row = row_number()) %>%
  pivot_wider(id_cols = c(variable, row), names_from = variable, values_from = value)
Ben
  • 28,684
  • 5
  • 23
  • 45