我想将数据帧列表中的数据格式化为百分比。这些值是因子。
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]])))))
这可以单独很好地工作,但不幸的是,它不会:
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)
}))
答案 0 :(得分:2)
不要将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%
您可以根据需要更改列名称。