James
James

Reputation: 107

Different ways of selecting columns inside function resulting in different results, why?

I have written a short function to clean some dataframes that I have in a list. When selecting columns using the df[,1] method, my function doesn't work. However when I select using df$Column it does. Why is this?

columns_1 <- function(x) {
  x[,1] <- dmy_hm(x[,1])
  x[,2] <- NULL
  x[,3] <- as.numeric(x[,3])
  x[,4] <- NULL
  return(x)
}

MS_ <- lapply(MS_, columns_1)


columns_2 <- function(x) {
  x$DateTime <- dmy_hm(x$DateTime)
  x$LogSeconds <- NULL
  x$Pressure <- as.numeric(x$Pressure)
  x$Temperature <- NULL
  return(x)
}

MS_ <- lapply(MS_, columns_2)

The function columns_2 produces the desired results (all dataframes in list are cleaned). columns_1 returns the error message:

Error in FUN(X[[i]], ...) : 
  (list) object cannot be coerced to type 'double'
In addition: Warning message:
All formats failed to parse. No formats found.

Upvotes: 0

Views: 37

Answers (1)

akrun
akrun

Reputation: 887118

The issue would be that the assignment was carried out after the first run and here some columns were lost.

library(lubridate)
MS_ <- lapply(MS_, columns_1)

Instead, it can be done by assigning to a different object

MS2_ <- lapply(MS_, columns_1)

data

set.seed(24)
df1 <- data.frame(DateTime = format(Sys.Date() + 1:5, "%d-%m-%Y %H:%M"),
    LogSeconds = 1:5, 
    Pressure = rnorm(5), Temperature = rnorm(5, 25),
             stringsAsFactors = FALSE)
MS_ <- list(df1, df1)

Upvotes: 2

Related Questions