R lapply() : Change all columns within all data frames in a list to numeric, 然后将所有值转换为百分比

标签 r lapply

题:

我对如何为数据框列表中的列批量处理 as.numeric() (或任何其他函数)感到有些困惑。

我知道我可以使用以下方法查看此列表中的特定数据框或列:

> my.list[[1]] 
# or columns within this data frame using:
> my.list[[1]][1]

但是当我尝试将其应用于 lapply() 函数以将所有数据从整数更改为数字时,我的麻烦就来了。
# Example of what I am trying to do
> my.list[[each data frame in list]][each column in data frame] <- 
as.numberic(my.list[[each data frame in list]][each column in data frame])

如果您能以任何方式帮助我,或者知道任何可以帮助我的资源,我将不胜感激。

背景:

我的数据框的结构如下例所示,其中我有 5 种栖息地类型以及有关单个物种家庭范围扩展到 n 的面积的信息:
# Example data
spp.1.data <- data.frame(Habitat.A = c(100,45,0,9,0), Habitat.B =  c(0,0,203,45,89), Habitat.C = c(80,22,8,9,20), Habitat.D = c(8,59,77,83,69), Habitat.E = c(23,15,99,0,10))

我有多个具有上述结构的数据框,它们已分配给列表对象:
all.spp.data <- list(spp.1.data, spp.2.data, spp.1.data...n)

然后我试图将所有数据框强制转换为 as.numeric() 以便我可以创建 % 栖息地使用的数据框,即:
# data, which is now numeric as per Phil's code ;)

 data.numeric <- lapply(data, function(x) {
  x[] <- lapply(x, as.numeric)
  x
   })

> head(data.numeric[[1]])
  Habitat.A Habitat.B Habitat.C Habitat.D Habitat.E
1       100         0        80         8        23
2        45         0        22        59        15
3         0       203         8        77        99
4         9        45         9        83         0
5         0        89        20        69        10

编辑:我想对所有数据框中的每一行求和
# Add row at the end of each data frame populated by rowSums()

 f <- function(i){
      data.numeric[[i]]$Sums <- rowSums(data.numeric[[i]])
      data.numeric[[i]]
  }

data.numeric.SUM <- lapply(seq_along(data.numeric), f)
head(data.numeric.SUM[[1]])

 Habitat.A Habitat.B Habitat.C Habitat.D Habitat.E     Sums
1       100         0        80         8        23   211
2        45         0        22        59        15   141
3         0       203         8        77        99   387
4         9        45         9        83         0   146
5         0        89        20        69        10   188

编辑:这是我用来将数据框中的值转换为使用的栖息地百分比的代码
# Used Phil's logic to convert all numbers in percentages

data.numeric.SUM.perc <- lapply(data.numeric.SUM, 
function(x) {
x[] <- (x[]/x[,6])*100
x
})

 Perc.Habitat.A Perc.Habitat.B Perc.Habitat.C Perc.Habitat.D Perc.Habitat.E
1             47             32              0              6              0
2              0              0             52             31             47
3             38             16              2              6             11
4              4             42             20             57             37
5             11             11             26              0              5
6            100            100            100            100            100

这仍然不是最简洁的方法,但它对我有用。

感谢 Phil、Val 和 Leo P 帮助解决这个问题。

最佳答案

我会更明确地这样做:

all.spp.data <- lapply(all.spp.data, function(x) {
  x[] <- lapply(x, as.numeric)
  x
})

作为个人偏好,这清楚地向我传达了我正在遍历数据框中的每一列,并遍历列表中的每个数据框。

关于R lapply() : Change all columns within all data frames in a list to numeric, 然后将所有值转换为百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44542801/

相关文章:

r - Bioconductor、R 版本和本地安装

r - 来自 qplot 的直方图数据

r - data.table[,-c(...)] 在包中定义时表现不同

r - 拆分、应用线性模型、组合

R循环/lapply,使用group by进行累计总计

r - 从 R 中的 data.table 列计算中位数

r - lapply 是否有序应用函数?

javascript - 为什么我的输出与导航栏页面中的标题重叠

R:将列表数据转换为数据框

R中的Reduce()超过导致错误的相似变量名称