重命名列表中的序列数字数据框列

时间:2018-07-18 16:44:52

标签: r list dataframe renaming

我有一个带有其他4个列表的列表,每个列表都有7个数据帧(具有36行和2列)。我只想按升序将数字序列的数据帧的第二列重命名,我尝试这样做:

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

其中“ data.precip”是4的第一个列表,因此列表“ data.Precip”中数据帧的秒列名称具有以下内容: “ PRECIPITACION_1” ....“ PRECIPITACION_7”。

现在,对于其他列表,我想重命名第二列,但按其他顺序重命名,从8开始,即“ PRECIPITACION_8” ...“ PRECIPITACION_14”

对于具有7个DF的第三个列表,请执行相同操作。

我的代码是:

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)
})
})

但是此代码不起作用,对此有任何想法吗?

1 个答案:

答案 0 :(得分:1)

考虑使用所需的数字构建相应的相同结构和长度的对象 my.nums ,然后运行双Map(包装到mapply)。这避免了使用i=i+1顺序增长和调用整数变量的需要。下面用随机数据进行演示。

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)

输出

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"