在R中绘图之前标签变量的功能

时间:2017-09-08 12:29:40

标签: r function label

我希望,这个问题对于这个论坛来说并不容易(实际上,我在这里提出这个问题几乎有点尴尬,但我整天都在讨论这个小问题...... )

我的数据框如下所示:

df <- data.frame(runif(4),
             c("po", "pr", "po", "pr"),
             c("Control 1","Control 1", "Treatment 1", "Treatment 1"))

names(df) <- list("values", "test_type", "group")

现在,我想轻松重新标记变量&#34; test_type&#34;和&#34;组&#34;之后的情节。 (它更好阅读&#34;预测试&#34;而不是&#34; pr&#34;在演示文稿中:-)) 我可以手动完成:

df$test_type <- factor(df$test_type,
                     levels = c("pr", "po"),
                     labels = c("pretest", "posttest"))
df$group <- factor(df$group,
                 levels = c("Control 1", "Treatment 1"),
                 labels = c("control", "EST"))

在这种情况下,我将不得不重复这个以获得更多数据帧,这导致我编写一个函数:

var_label <- function(df, test, groups){

# Create labels
df$test_type <- factor(df$test,
                     levels = c("pr", "po"),
                     labels = c("pretest", "posttest"))
df$group <- factor(df$groups,
                 levels = c("Control 1", "Treatment 1"),
                 labels = c("control", "EST"))

return(list(df$test_type, df$group))
}

不幸的是,这不起作用。我尝试了很多不同的版本和Hmisc包中的不同命令,但这些都没有奏效。我知道,我可以用另一种方式解决这个问题,但我尝试编写更有效和更短的代码,并且真的很感兴趣,我必须改变以使这个功能起作用。或者你有更好的建议吗?

提前多多谢谢!!

1 个答案:

答案 0 :(得分:0)

正如我上面提到的,我认为forcats::fct_relabel()就是你想要的,以及dplyr::mutate_at()。假设您的重新标记需求并不比您的问题中所概述的更复杂,以下内容应该可以帮助您了解您正在寻找的内容。

####BEGIN YOUR DATAFRAME CREATION####
df <- data.frame(runif(4),
                 c("po", "pr", "po", "pr"),
                 c("Control 1","Control 1", "Treatment 1", "Treatment 1"))

names(df) <- list("values", "test_type", "group")
#####END YOUR DATAFRAME CREATION#####

# Load dplyr and forcats
library(dplyr)
library(forcats)

# create a map of labels and levels based on your implied logic
# the setup is label = level
label_map <- c("pretest" = "pr"
               ,"posttest" = "po"
               ,"control" = "Control 1" 
               ,"EST" = "Treatment 1")

# create a function to exploit the label map
fct_label_select <- function(x, map) {
  names(which(map == x))
}

# create a function which is responsive to a character vector
# as required by fct_relabel
fct_relabeler <- function(x, map) {
  unlist(lapply(x, fct_label_select, map = map))
}

fct_relabeler(levels(df$test_type), map = label_map)

# function to meet your apparent needs
var_label <- function(df, cols, map){
  df %>%
    mutate_at(.vars = cols
              ,.fun = fct_relabeler
              ,map = map)
}

var_label(df = df, cols = c("test_type", "group"), map = label_map)

# values test_type   group
# 1 0.05159681  posttest control
# 2 0.89050323   pretest control
# 3 0.42988881  posttest     EST
# 4 0.32012811   pretest     EST