Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert long table to wide table in r?

I am trying to format my data so that it is wide rather than long and I seem to be encountering issues transforming it from a list to anything that would let me change its form.

library(stringr)
library(tidyr)
library(dplyr)


A <- as.list(c("v", "w", "x", "y", "NA"))
str(A) 
X <- 5
G <- str_split_fixed(A, ", ", X)
G <- as.data.frame(A)
G <- G[-c(182, 183)] #irrelevant on example data my actual data is much longer
P <- G
P <- gsub("NA", "Z", as.character(P))#also irrelavent for example data
P <- str_split_fixed(P, ", ", X)
colnames(P) <-  c("A", "B", "C", "D", "E")
Titles <- c("Time_Modifier", "Times", "Scrambles", "Blank", "ID")
T.1 <- strsplit(Titles, " ")
n <- nrow(P)
n/5
L <- rep(T.1, n/5)
T.T <- cbind(P, L)

T.T.1 <- subset(T.T, select = -c(B, C, D, E))
T.T.1 <- subset(T.T.1, L != "Blank")

I have tried using pivot_wider() where I get this error

pivot_wider(T.T.1,
           names_from = L,
           values_from = A
           )

Error in UseMethod("pivot_wider") :
no applicable method for ‘pivot_wider’ applied to an object of class "c(‘matrix’, ‘array’, ‘list’)"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have also tried using pivot_wider_spec() where I get a similar error. which lead me to think that T.T.1 is being stored as a list; which low and behold it is. however, I seem to be unable to change T.T.1 into a data type that pivot_wider likes.

I have tried transforming T.T.1 using as.data.frame(), as.character and just about everything else I could think of or find.

T.T.1 <- as.data.frame(T.T.1)
pivot_wider(T.T.1,
           names_from = L,
           values_from = A
           )

Error: Must subset columns with a valid subscript vector.
x Subscript has the wrong type list.
i It must be numeric or character.

T.T.1 <- as.character(T.T.1)
pivot_wider(T.T.1,
           names_from = L,
           values_from = A
           )

Error in UseMethod("pivot_wider") :
no applicable method for ‘pivot_wider’ applied to an object of class "character"

any and all help is very much appreciated.

>Solution :

pivot_wider needs a data frame:

Therefore use as_tibble or as.data.frame before T.T.1 which is a matrix (you can check with class(T.T.1):

You will get a list. In case you want to see the list use unnest from tidyr package.

pivot_wider(as_tibble(T.T.1),
            names_from = L,
            values_from = A
) %>% 
  unnest(cols = c(Time_Modifier, Times, Scrambles, ID))

output:

  Time_Modifier Times Scrambles ID   
  <chr>         <chr> <chr>     <chr>
1 v             w     x         Z    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading