mardi 22 décembre 2020

renaming the columns in each dataframe in a list with, while the number of columns vary

I am hoping to create a short if-else (or other if this is a stupid approach) function for renaming the columns of all dataframes within a list. I do see many similar questions, which have helped me get my code this far, but they deviate in some specifics leaving me with an outstanding problem. I am really hoping to do this without a package (as silly as that seems given tidyverse is already loaded...)

Libraries- should be unnecessary.

library(raster)
library(tidyverse)
library(sf)
library(data.table)

Example data:

player <- list(`10` = structure(list(ID = "10", integer_val = 1028, coords.x1 = 266781.960994547, 
    coords.x2 = 4208220.00292058, X1979_08 = 18.1568756103516, 
    X1979_09 = 21.3082904815674, X1979_10 = 14.6396112442017, 
    X1979_11 = 5.12351417541504, X1979_12 = 6.30002880096436, 
    X1980_01 = 4.20031881332397, X1980_02 = 5.70116329193115, 
    X1980_03 = 5.33258485794067, X1980_04 = 8.69955348968506, 
    X1980_05 = 9.88166332244873, X1980_06 = 22.8935871124268, 
    X1980_07 = 24.7441787719727), row.names = 1L, class = "data.frame"), 
    `10015` = structure(list(ID = "10015", integer_val = 811, 
        coords.x1 = 432912.236471687, coords.x2 = 4418957.6628215, 
        X1961_09 = 9.60138702392578, X1961_10 = 9.8080587387085, 
        X1961_11 = 3.80465173721313, X1961_12 = 2.24590229988098, 
        X1962_01 = 2.89834785461426, X1962_02 = 4.26397132873535, 
        X1962_03 = 4.47179555892944, X1962_04 = 8.64332294464111, 
        X1962_05 = 11.5554437637329, X1962_06 = 15.2697010040283), row.names = 891L, class = "data.frame"))

Now I have already created names for each list (generally 6k-7k baby dataframes per list)

column_names <- c('ID', 'integer_val', 'longitude', 'latitude', '12', '11', '10', '09', '08', '07', '06', '05', '04', '03', '02', '01')
colnames_vpd <- c(column_names[1:4], paste("vpd", column_names[5:16], sep = "-"))

Some may argue this approach is unnecessary, but the renaming occurs on some lists that are output from a function that takes a night to run, so I just try to hit run and go to sleep...

Names_func <- function(x, y){
  if(ncol(x) == 16)
    {colnames(x) <- y}
  else{colnames(x) <- y[1:(ncol(x)-1)]
  }
}

test <- lapply(X = listabc, FUN =  Names_func, y = colnames_vpd)

So the problem with my function is that it overwrites not just the column names, but all of the data in each dataframe. I have tried many permutations on this, but to no avail. Afterwards these data are converted to a d.t. with rbindlist and fill.

I do know that a very similar results can be done on the fly with dplyr's rename, but I would prefer a base solution if possible. Thanks.

EDIT: I am quite convinced I am dumber than a rock. Apologies if you have read to here.

Names_func <- function(x, y){
  if(ncol(x) == 16)
    {names(x) <- y}
  else{names(x) <- y[1:(ncol(x))]
  }
  return(x)
}

Aucun commentaire:

Enregistrer un commentaire