在 R 中的 for 循环中应用 trimws() 时出错

Error when applying trimws() inside a for-loop in R

我正在尝试对 R 中的 df 列表中包含的每个 df 的每一列的每一行应用 trimws() 函数。

这是我的代码:

for(i in 1:length(df_list)){
  for (j in i) {
    for(z in j){
      df_list[[i]][[j]][[z]] <- 
        trimws(df_list[[i]][[j]][[z]])
    }
    
  }  
  
}

控制台输出:

Error in .subset2(x, i, exact = exact) : subscript out of bounds

还有其他方法可以应用这个功能吗?

我们可以通过使用 lapply 遍历 list 然后使用 lapply 逐列应用 trimws 来使其更简单(因为 trimws 是向量化的)

df_list1 <- lapply(df_list, function(dat) {
                 dat[] <- lapply(dat, trimws)
                dat
         })

通过j in iz in j,假设listlengthlist的列数和行数相同每个数据集,这可能不是这种情况,因此导致下标越界错误。

根据下面的示例数据,length为10,但nrowncol小于10,导致下标错误

for(i in 1:length(df_list)){
   for (j in i) {
     for(z in j){
       df_list[[i]][[j]][[z]] <- 
         trimws(df_list[[i]][[j]][[z]])
     }
     
   }  
   
 }

Error in .subset2(x, i, exact = exact) : subscript out of bounds

数据

df_list <- list(structure(list(col1 = c("a ", "b", "c "), col2 = c("b ", 
"d", "f")), class = "data.frame", row.names = c(NA, -3L)), structure(list(
    col1 = c("a ", "b", "c ", "d"), col2 = c("b ", "d", "f", 
    "g "), col3 = c(" f", "d", "m ", "c")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(col1 = c("a ", "b", "c "), col2 = c("b ", 
"d", "f")), class = "data.frame", row.names = c(NA, -3L)), structure(list(
    col1 = c("a ", "b", "c ", "d"), col2 = c("b ", "d", "f", 
    "g "), col3 = c(" f", "d", "m ", "c")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(col1 = c("a ", "b", "c "), col2 = c("b ", 
"d", "f")), class = "data.frame", row.names = c(NA, -3L)), structure(list(
    col1 = c("a ", "b", "c ", "d"), col2 = c("b ", "d", "f", 
    "g "), col3 = c(" f", "d", "m ", "c")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(col1 = c("a ", "b", "c "), col2 = c("b ", 
"d", "f")), class = "data.frame", row.names = c(NA, -3L)), structure(list(
    col1 = c("a ", "b", "c ", "d"), col2 = c("b ", "d", "f", 
    "g "), col3 = c(" f", "d", "m ", "c")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(col1 = c("a ", "b", "c "), col2 = c("b ", 
"d", "f")), class = "data.frame", row.names = c(NA, -3L)), structure(list(
    col1 = c("a ", "b", "c ", "d"), col2 = c("b ", "d", "f", 
    "g "), col3 = c(" f", "d", "m ", "c")), class = "data.frame", row.names = c(NA, 
-4L)))