将嵌套列表的单个元素从因子转换为字符

问题描述 投票:0回答:2

我通过读取目录中的所有csv文件创建了一个名为“mydata”的嵌套列表:

> temp <- list.files(pattern="*.csv")
> ls <- lapply(temp, read.csv)
> str(mydata)
List of 4
 $ :'data.frame':   51 obs. of  5 variables:
  ..$ Primary.ID : Factor w/ 862 levels "X102.034169503712_9.041775",..: 564 621 527 260 566 590 625 68 699 95 ...
  ..$ PosORNeg   : Factor w/ 2 levels "Neg","Pos": 2 1 2 1 2 1 2 1 1 2 ...
  ..$ p.1.P      : num [1:51] -0.435 -0.278 -0.285 0.31 -0.233 ...
  ..$ p.corr..1.P: num [1:51] -0.845 -0.704 -0.598 0.644 -0.818 ...
  ..$ VIP.2.     : num [1:51] 13.17 8.24 7.76 7.69 6.84 ...
 $ :'data.frame':   32 obs. of  5 variables:
  ..$ Primary.ID : Factor w/ 862 levels "X102.034169503712_9.041775",..: 564 621 527 590 566 358 563 571 625 12 ...
  ..$ PosORNeg   : Factor w/ 2 levels "Neg","Pos": 2 1 2 1 2 1 2 1 2 1 ...
  ..$ p.1.P      : num [1:32] -0.468 -0.301 -0.233 -0.183 -0.142 ...
  ..$ p.corr..1.P: num [1:32] -0.916 -0.813 -0.461 -0.502 -0.705 ...
  ..$ VIP.4.     : num [1:32] 13.56 8.58 7.02 5.65 4.03 ...
 $ :'data.frame':   44 obs. of  5 variables:
  ..$ Primary.ID : Factor w/ 862 levels "X102.034169503712_9.041775",..: 564 527 232 381 621 590 566 54 625 40 ...
  ..$ PosORNeg   : Factor w/ 2 levels "Neg","Pos": 2 2 1 2 1 1 2 1 2 1 ...
  ..$ p.1.P      : num [1:44] -0.415 -0.323 0.291 -0.263 -0.279 ...
  ..$ p.corr..1.P: num [1:44] -0.919 -0.701 0.681 -0.518 -0.851 ...
  ..$ VIP.2.     : num [1:44] 11.29 8.7 8.6 7.61 7.33 ...
 $ :'data.frame':   43 obs. of  5 variables:
  ..$ Primary.ID : Factor w/ 862 levels "X102.034169503712_9.041775",..: 564 621 232 92 210 114 473 563 252 95 ...
  ..$ PosORNeg   : Factor w/ 2 levels "Neg","Pos": 2 1 1 2 2 2 2 2 1 2 ...
  ..$ p.1.P      : num [1:43] -0.416 -0.333 -0.251 0.189 0.168 ...
  ..$ p.corr..1.P: num [1:43] -0.73 -0.753 -0.563 0.783 0.729 ...
  ..$ VIP.2.     : num [1:43] 11.05 8.76 8.05 5.3 5.11 ...

我想编写代码来将每个嵌套列表中的每个'Primary.ID'列从因子转换为字符,但是无法弄清楚如何执行此操作。此外,我从中读取了目录中有4个csv文件,但我也希望代码能够容纳动态数量的csv文件。

谢谢!

r list dataframe
2个回答
1
投票

对基本代码进行一些调整可以解决您的问题:

temp <- list.files(pattern="*.csv")
ls <- lapply(files, function(x) read.csv(x, stringsAsFactors = FALSE))

同样,tidyverse解决方案可能是:

library(tidyverse)

temp <- list.files(pattern="*.csv")
ls <- lapply(files, read_csv)

1
投票

我喜欢tomasu的答案比我的好。不过,这里有一种方法可以在读取数据后将factor转换为字符。

首先,我创建了一些toydata:

mydata <- list(data.frame(col_fac = letters[1:3], y = 3:5), 
               data.frame(col_fac = letters[4:6], z = 101:103))
str(mydata)
List of 2
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ col_fac: Factor w/ 3 levels "a","b","c": 1 2 3
  ..$ y      : int [1:3] 3 4 5
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ col_fac: Factor w/ 3 levels "d","e","f": 1 2 3
  ..$ z      : int [1:3] 101 102 103

然后使用tidyverse包:

library(tidyverse)
mydata2 <- map(mydata, ~mutate(.x, col_fac = as.character(col_fac)))
str(mydata2)
List of 2
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ col_fac: chr [1:3] "a" "b" "c"
  ..$ y      : int [1:3] 3 4 5
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ col_fac: chr [1:3] "d" "e" "f"
  ..$ z      : int [1:3] 101 102 103
© www.soinside.com 2019 - 2024. All rights reserved.