Error with dcast

Hello, I was using a script to transform a data table from long to wide format , but now it is not working anymore and I have not touched anything.

The code is:

rm(list= ls())

#Cargar el dataset del .csv

df.long <- read.csv("Flywheel datos fase CON.csv", #Ajustar archivo csv
                    header = TRUE,
                    sep = ";")

#Pasar de long a wide format 


library(tidyr)
library(dplyr)
library(data.table)
library(reshape2)

col_names <- c(colnames(df.long, do.NULL = FALSE, prefix = "col"))
col_names <- col_names [3:19] #Ajustar el rango de columnas para pasar a formato wide
setDT(df.long)
df.wide <- dcast(df.long, Group ~ Momento_de_inercia  , # Ajustar las variables que se mantienen ~ y la variable que agrupa 
                 value.var = c(col_names))

#df.wide.variable <- df.wide %>% dplyr:: select(contains( c("CON_RAD_AVG")))
#df.wide.variable <- df.wide.variable %>% dplyr:: select(-contains( c("AVG_RAD100_90")))

#Grabar archivo .csv

write.csv2(df.wide, "Flywheel datos fase CON wide.csv")

And the error I get is:

Error in .subset2(x, i, exact = exact) :
falló indexación recursiva en nivel 2
Además: Warning message:
In if (!(value.var %in% names(data))) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado

Any help will be appreciated.

Clearly we don't have your files, so we cannot recreate your code.

However, I would really advise you to understand your code rather than just copy and paste various bits. You have multiple libraries loaded with overlapping functionality which is usually a sign of confusion about the code.

For instance, reshape2 has been superseded by tidyr and data.table can do what dplyr and tidyr do.

This explains how to use data.table's dcast():
Efficient reshaping using data.tables • data.table (rdatatable.gitlab.io)

1 Like

This topic was automatically closed 21 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.