下面是一个简化的示例:
library(tidyverse)
frame <- tribble(
~a, ~b, ~c,
1, 1, 2,
5, 4, 7,
2, 3, 4,
3, 1, 6
)
key <- tribble(
~col, ~name, ~type, ~labels,
1, "a", "f", c("one", "two", "three", "four", "five"),
2, "b", "f", c("uno", "dos", "tres", "cuatro"),
3, "c", "f", 1:7
)
是否有一种优雅的方法可以在frame
中以编程方式扫描列并根据key
中的参数应用特定因子类?预期结果将是:
# A tibble: 4 x 3
a b c
<fctr> <fctr> <fctr>
1 one uno 2
2 five cuatro 7
3 two tres 4
4 three uno 6
到目前为止,我最好的解决方案是使用purrr
的map2()
但分配不是 IMO 最优雅的:
frame[key$col] <- map2(key$col, key$labels,
function(x, y) factor(frame[[x]], levels = 1:length(y), labels = y))
有没有人有更整洁的解决方案?请注意,我的原始数据框有数百列,我需要用不同的级别/标签重构其中的大部分,因此该过程必须自动化。
这是另一种解决方案。我不确定它有多"优雅"。希望有人可以对此进行改进。
suppressPackageStartupMessages(library(tidyverse))
frame <- tribble(
~a, ~b, ~c,
1, 1, 2,
5, 4, 7,
2, 3, 4,
3, 1, 6
)
key <- tribble(
~col, ~name, ~type, ~labels,
1, "a", "f", c("one", "two", "three", "four", "five"),
2, "b", "f", c("uno", "dos", "tres", "cuatro"),
3, "c", "f", 1:7
)
colnames(frame) %>%
map(~ {
factor(pull(frame, .x),
levels = 1:length(pluck(key[key$name == .x, "labels"], 1, 1)),
labels = pluck(key[key$name == .x, "labels"], 1, 1))
}) %>%
set_names(colnames(frame)) %>%
as_tibble()
#> # A tibble: 4 x 3
#> a b c
#> <fctr> <fctr> <fctr>
#> 1 one uno 2
#> 2 five cuatro 7
#> 3 two tres 4
#> 4 three uno 6
我很想知道为此提出了哪些其他解决方案。我唯一的建议是稍微更改建议的解决方案,以便更清楚地表明frame
将以某种方式进行修改,而不是将其保留在map2
使用的函数的主体中。
例如,在调用map2
时将frame
作为附加参数传递:
frame[key$col] <- map2(key$col, key$labels,
function(x, y, z) factor(z[[x]], levels = 1:length(y), labels = y),
frame)
或者使用管道运算符%>%
执行相同的操作:
frame[key$col] <- frame %>%
{ map2(key$col, key$labels,
function(x, y, z) factor(z[[x]], levels = 1:length(y), labels = y), .) }
我不知道这个答案是否满足您对整洁的要求,因为它使用了普通的旧 for 循环。但它可以完成这项工作,在我看来,它易于阅读/理解以及相当快。
library(tidyverse)
frame <- tribble(
~a, ~b, ~c,
1, 1, 2,
5, 4, 7,
2, 3, 4,
3, 1, 6
)
key <- tribble(
~col, ~name, ~type, ~labels,
1, "a", "f", c("one", "two", "three", "four", "five"),
2, "b", "f", c("uno", "dos", "tres", "cuatro"),
3, "c", "f", 1:7
)
for (i in 1:nrow(key)) {
var <- key$name[[i]]
x <- frame[[var]]
labs <- key$labels[[i]]
lvls <- 1:max(length(x), length(labs)) # make sure to have the right lengths
frame <- frame %>% mutate(!! var := factor(x, levels = lvls, labels = labs))
}
frame
#> # A tibble: 4 x 3
#> a b c
#> <fctr> <fctr> <fctr>
#> 1 one uno 2
#> 2 five cuatro 7
#> 3 two tres 4
#> 4 three uno 6
典型的整洁方法是重塑数据以将所有变量放在一列中,然后将函数应用于该列,最后将其重塑为原始格式。但是,因素并不真正喜欢这样,因此我们需要使用其他方式。因素甚至被认为是整洁的吗?
编辑
关于我假设 for 循环类似于map2
函数,我错了。
以下是一些基准:
library(microbenchmark)
frame1 <- frame
frame2 <- frame
microbenchmark(
map2 = {
frame1[key$col] <- map2(key$col, key$labels,
function(x, y) factor(frame[[x]],
levels = 1:max(frame[[x]],
length(y)),
labels = y))
},
forloop = {
for (i in 1:nrow(key)) {
var <- key$name[[i]]
x <- frame2[[var]]
labs <- key$labels[[i]]
lvls <- 1:max(length(x), length(labs))
frame2 <- frame2 %>% mutate(!! var := factor(x, levels = lvls, labels = labs))
}
}
)
# Unit: microseconds
# expr min lq mean median uq max neval cld
# map2 375.53 416.5805 514.3126 450.825 484.2175 3601.636 100 a
# forloop 11407.80 12110.0090 12816.6606 12564.176 13425.6840 16632.682 100 b
对于此问题,可以使用基本 R 代码:
(A=`names<-`(data.frame(mapply(function(x,y)x[y],key$labels,frame)),key$name))
a b c
1 one uno 2
2 five cuatro 7
3 two tres 4
4 three uno 6
sapply(A,class)
a b c
"factor" "factor" "factor"