Error when trying to mutate a dataframe containing lists

44 views Asked by At

I have a dataframe containing lists, which looks similar to the following with the exception that the original has a lot more elements and rows:

 p                        stim                                    time 
list("001", 240, 258)    list(c(289.88, 294.830, 289.88, 894.83))  list(c(889.88, 894.830))
list("001", 240, 258)    list(c(299.88, 896.830, 200.88, 894.83))  list(c(989.88, 
354.850))
etc

This information came from a series of files. I am trying to get this information into a .txt file. I am using mutate in an attempt to coerce the dataframe to display something similar to the following:

$stim
[1] 289.88, 294.830, 289.88, 894.83


$stim
[1] 299.88, 896.830, 200.88, 894.83

Here is the code I am using to do this:

library(raveio)
base :: setwd("filepath")
filenames <- base::list.files(
  path = ("filepath"), 
  recursive = TRUE,
  pattern = "*.mat"
)

#CONVERT DATA TO LIST
mylist <- lapply(filenames, function(x) read_mat(x))

#CONVERT LIST TO DATAFRAME
library(tidyverse)
mydata <- map_dfr(mylist, ~as.data.frame(t(.)))

df <- mydata%>%
  mutate(across(everything(), ~sapply(.,paste0, collpase = ",")))

#WRITE DATA TO .txt FILE
sink("mydata.txt")
print(df)
sink()

Everything works perfectly until I run the mutate function. Then, I get the following error:

Error in `mutate()`:
ℹ In argument: `across(everything(), ~sapply(., paste0, collpase = ","))`.
Caused by error in `across()`:
! Can't compute column `time`.
Caused by error in `dplyr_internal_error()`:
Run `rlang::last_trace()` to see where the error occurred.

I realize that I am probably taking a very roundabout way to do all this, but historically, it's what has worked for my purposes. How do I go about resolving the error in the mutate function?

0

There are 0 answers