r - 将数据框列表中的值格式化为百分比
问题描述
我想将数据框列表中的数据格式化为百分比。值是因素。
df.ls <- list(list(id = c(1,2,3), x = c(4,5,6), works = c(7,8,9)),
list(id = c(10,11,12), x = c(13,14,15), works = c(16,17,18)))
为此,我创建了自定义百分比格式:
library(scales)
my_perc_format=percent_format(accuracy = .01, scale = 100,
suffix = "%",decimal.mark = ".")
然后我尝试将它应用到我的列表中,将值格式化为数字:
test=lapply(df.ls, function(x)
my_perc_format(as.numeric(as.character(unlist(df.ls[[x]])))))
单独地,这完美地工作,但在 lapply 它不会:
my_perc_format(as.numeric(as.character(unlist(df.ls[[1]]))))
编辑:
这些值现在是因素,但如果可能的话,我想在转换为百分比时成为数字。
编辑:
这是另一个尝试转换我的数据。这一次它是一个因素。没有relist()
输出很好,但不是想要的结构。随着relist()
我得到想要的结构,但它返回NA。
df.ls <- list(list(id = as.factor(c("1","2","3")), x = as.factor(c("4","5","6")), works = as.factor(c("7","8","9"))),
list(id = as.factor(c("10","11","12")), x = as.factor(c("13","14","15")), works = as.factor(c("16","17","18"))))
names(df.ls)=c("list1","list2")
test=as.data.frame(sapply(df.ls, function(x){
relist(my_perc_format(as.numeric(as.character(unlist(x)))),x)
}))
解决方案
不要直接对正在lapply
使用的列表进行子集化x
。
lapply(df.ls, function(x) my_perc_format(as.numeric(as.character(unlist(x)))))
#[[1]]
#[1] "100.00%" "200.00%" "300.00%" "400.00%" "500.00%" "600.00%" "700.00%" "800.00%" "900.00%"
#[[2]]
#[1] "1 000.00%" "1 100.00%" "1 200.00%" "1 300.00%" "1 400.00%" "1 500.00%" "1 600.00%" "1 700.00%" "1 800.00%"
要将输出作为数据帧列表,我们可以这样做
lapply(df.ls, function(x) {
vals <- unlist(x)
data.frame(original = vals, value = my_perc_format(vals), row.names = NULL)
})
#[[1]]
# original value
#1 1 100.00%
#2 2 200.00%
#3 3 300.00%
#4 4 400.00%
#5 5 500.00%
#6 6 600.00%
#7 7 700.00%
#8 8 800.00%
#9 9 900.00%
#[[2]]
# original value
#1 10 1 000.00%
#2 11 1 100.00%
#3 12 1 200.00%
#4 13 1 300.00%
#5 14 1 400.00%
#6 15 1 500.00%
#7 16 1 600.00%
#8 17 1 700.00%
#9 18 1 800.00%
或者为了保持与原始列表相同的结构,我们可以使用relist
lapply(df.ls, function(x) {
relist(my_perc_format(unlist(x)), x)
})
#[[1]]
#[[1]]$id
#[1] "100.00%" "200.00%" "300.00%"
#[[1]]$x
#[1] "400.00%" "500.00%" "600.00%"
#[[1]]$works
#[1] "700.00%" "800.00%" "900.00%"
#[[2]]
#[[2]]$id
#[1] "1 000.00%" "1 100.00%" "1 200.00%"
#[[2]]$x
#[1] "1 300.00%" "1 400.00%" "1 500.00%"
#[[2]]$works
#[1] "1 600.00%" "1 700.00%" "1 800.00%"
编辑
as.data.frame(lapply(df.ls, function(x) {
temp = factor(my_perc_format(as.numeric(as.character(unlist(x)))))
split(temp, rep(seq_along(x) , lengths(x)))
}))
# list1.1 list1.2 list1.3 list2.1 list2.2 list2.3
#1 100.00% 400.00% 700.00% 1 000.00% 1 300.00% 1 600.00%
#2 200.00% 500.00% 800.00% 1 100.00% 1 400.00% 1 700.00%
#3 300.00% 600.00% 900.00% 1 200.00% 1 500.00% 1 800.00%
您可以根据需要更改列名。
推荐阅读
- django - 我们可以在单个 Django 表单字段中同时使用自定义验证(使用 clean())和内置验证器吗?如果/如果-不是如何/为什么?
- python - 我收到此错误 unsupported operand type(s) for -: 'method' and 'int'
- html - 从 index.html 滚动页面中的锚链接中删除主题标签
- java - 为 Map/Associative Array 数据结构实现 put(或 add)方法
- javascript - NodeJS / Express中的setTimeout抛出TypeError:无法读取未定义的属性'client'
- java - 当单元测试控制器我得到一个断言错误
- pytorch - 在 PyTorch 上转置:IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
- json - 如何用vue更改数据源?
- c# - xml命名空间的属性
- android - 而不是字符串方法返回 Kotlin.Unit