1

I have a list with other 4 list, each one of list have 7 data frames (with 36 rows and 2 column). I want rename only the second column of data frames with a sequence numeric in ascending order, i try this:

for (i in 1:length(data.Precip)) 
 colnames(data.Precip[[i]])[2] =  paste(colnames(data.Precip[[i]])[2],i , sep = "_")

where "data.precip" is a first list of the 4, so the names of seconds columns of data frame within a list "data.Precip" has this: "PRECIPITACION_1"...."PRECIPITACION_7".

Now, for the other list I want to rename the second column but other order, starting of number 8, i.e. "PRECIPITACION_8"..."PRECIPITACION_14"

For the third list with 7 DF do the same.

My code is:

my.data <- list(data.Precip, data.Rad, data.Velvi, data.Temp)
name.x <- c("Date")
i = i+1
name.y <- paste(colnames(my.data[i])[2], i, sep = "_")
my.data <- lapply(seq(my.data), function(i){
lapply(seq(name.y), function(j){
y <- data.frame(my.data[[i]][[j]])
 return(y)
})
})

But this code doesn't work, any idea for this?

1
  • Could you please post a version of your code with some toy dataset that will be runable if we just copy you code? That way we can figure out what you are asking and how to fix it much more easily. Commented Jul 18, 2018 at 17:04

1 Answer 1

1

Consider building a corresponding same structure and length object, my.nums, of needed numbers and then run a double Map (wrapper to mapply). This avoids the need to sequentially grow and recall an integer variable with i=i+1. Below demonstrates with random data.

set.seed(7182018)
my.data <- list(data.Precip = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE), 
                data.Rad = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE), 
                data.Velvi = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE), 
                data.Temp = replicate(7, data.frame(Date = 1:36, col = runif(36, 0, 50)), simplify = FALSE))

# LONG FORM
my.nums <- list(names.Precip = lapply(1:7, identity),
                names.Rad = lapply(8:14, identity),
                names.Velvi = lapply(15:21, identity),
                names.Temp = lapply(22:28, identity))    
# SHORT FORM
my.nums <- lapply(seq(1,28, by=7), function(x) lapply(seq(x, x+6), identity))

my.names <- list(names.Precip = lapply(rep("PRECIPITACIO_", 7), identity),
                 names.Rad = lapply(rep("RADIACION_", 7), identity),
                 names.Velvi = lapply(rep("VELOCIDAD.VIENTO_", 7), identity),
                 names.Temp = lapply(rep("TEMPERATURA_", 7), identity))
# FUNCTIONS
name_func <- function(df, m, n) setNames(df, c("Date", paste0(m, n)))
iter_df <- function(df_lst, m_list, n_lst) Map(name_func, df_lst, m_list, n_lst)

my.new.data <- Map(iter_df, my.data, my.names, my.nums)

Output

lapply(my.new.data, function(lst) lapply(lst, names))

$data.Precip
$data.Precip[[1]]
[1] "Date"           "PRECIPITACIO_1"

$data.Precip[[2]]
[1] "Date"           "PRECIPITACIO_2"

$data.Precip[[3]]
[1] "Date"           "PRECIPITACIO_3"

$data.Precip[[4]]
[1] "Date"           "PRECIPITACIO_4"

$data.Precip[[5]]
[1] "Date"           "PRECIPITACIO_5"

$data.Precip[[6]]
[1] "Date"           "PRECIPITACIO_6"

$data.Precip[[7]]
[1] "Date"           "PRECIPITACIO_7"


$data.Rad
$data.Rad[[1]]
[1] "Date"        "RADIACION_8"

$data.Rad[[2]]
[1] "Date"        "RADIACION_9"

$data.Rad[[3]]
[1] "Date"         "RADIACION_10"

$data.Rad[[4]]
[1] "Date"         "RADIACION_11"

$data.Rad[[5]]
[1] "Date"         "RADIACION_12"

$data.Rad[[6]]
[1] "Date"         "RADIACION_13"

$data.Rad[[7]]
[1] "Date"         "RADIACION_14"


$data.Velvi
$data.Velvi[[1]]
[1] "Date"                "VELOCIDAD.VIENTO_15"

$data.Velvi[[2]]
[1] "Date"                "VELOCIDAD.VIENTO_16"

$data.Velvi[[3]]
[1] "Date"                "VELOCIDAD.VIENTO_17"

$data.Velvi[[4]]
[1] "Date"                "VELOCIDAD.VIENTO_18"

$data.Velvi[[5]]
[1] "Date"                "VELOCIDAD.VIENTO_19"

$data.Velvi[[6]]
[1] "Date"                "VELOCIDAD.VIENTO_20"

$data.Velvi[[7]]
[1] "Date"                "VELOCIDAD.VIENTO_21"


$data.Temp
$data.Temp[[1]]
[1] "Date"           "TEMPERATURA_22"

$data.Temp[[2]]
[1] "Date"           "TEMPERATURA_23"

$data.Temp[[3]]
[1] "Date"           "TEMPERATURA_24"

$data.Temp[[4]]
[1] "Date"           "TEMPERATURA_25"

$data.Temp[[5]]
[1] "Date"           "TEMPERATURA_26"

$data.Temp[[6]]
[1] "Date"           "TEMPERATURA_27"

$data.Temp[[7]]
[1] "Date"           "TEMPERATURA_28"
Sign up to request clarification or add additional context in comments.

5 Comments

@Perfait thanks, this code worked perfectly, only one question more, its posible change the name "PRECIPITACION" BY "RADIACION" in the second list, "VELOCIDAD.VIENTO" at third list and "TEMPERATURA" for fourth list?
Sure you can. Simply, pass another nested list into the double Map calls. See edit. Do note: Map (mapply) can take an open-ended number of arguments, 2, 3, ... 80!
sorry, I don´t understand you, in the line iter_df <- function(df_lst, m_list, n_lst) Map(name_func, df_lst, m_list, n_lst) add other list?
I added a new my.names list and passed it in the functions as a new argument, m_list.
Oh, you already made the change the code for the question above? thanks @Parfait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.