简体   繁体   English

将数据帧列表中的值格式化为百分比

[英]Format values in list of data frames as percent

I want to format data in list of data frames as percentage. 我想将数据帧列表中的数据格式化为百分比。 The values are factors. 值是因子。

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

For that, I create custom percentage format: 为此,我创建了自定义百分比格式:

library(scales)
my_perc_format=percent_format(accuracy = .01, scale = 100,
                suffix = "%",decimal.mark = ".")

Then I try to apply it to my list, formatting the values as numbers: 然后,我尝试将其应用于列表,将值格式化为数字:

test=lapply(df.ls, function(x) 
    my_perc_format(as.numeric(as.character(unlist(df.ls[[x]])))))

Separately this works perfectly, but in lapply it wouldn't: 另外,这可以完美地工作,但不幸的是,它不能:

my_perc_format(as.numeric(as.character(unlist(df.ls[[1]]))))

Edit: 编辑:

这是列表中的一个数据帧

The values are factors now, but I want to be number when converted to percent, if it's possible. 这些值现在是因素,但如果可能的话,我希望将其转换为百分比时是数字。

EDIT: 编辑:

This is another try to convert my data. 这是另一种转换我的数据的尝试。 This time it's as factor. 这次是因素。 Without relist() the output is fine, but not in the wanted structure. 没有relist() ,输出会很好,但是不在所需的结构中。 With relist() I get the wanted structure, but it returns NA . 使用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)
}))

Don't subset the list in lapply use x directly. 不要在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%"

To get output as list of dataframes, we can do 为了获得输出作为数据帧列表,我们可以

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%

Or to maintain the same structure as original list we can use relist 或保持与原始列表相同的结构,我们可以使用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%"

EDIT 编辑

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%

You can change the column names as needed. 您可以根据需要更改列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM