首页 > 解决方案 > 将数据框列表中的值格式化为百分比

问题描述

我想将数据框列表中的数据格式化为百分比。值是因素。

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)
}))

标签: rformat

解决方案


不要直接对正在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%

您可以根据需要更改列名。


推荐阅读