1

下面是两个简单的数据框。我想重新编码(折叠)Sat1Sat2列,以便所有满意程度都简单编码为Satisfied,所有不满意程度都编码为Dissatisfied。中性将保持中性。因此,这些因素将具有三个级别 - Satisfied, Dissatisfied, and Neutral

我通常会通过绑定数据帧并与包lapply中的重新代码一起使用来完成此操作car,例如:

  DF1[2:3] <- lapply(DF1[2:3], recode, c('"Somewhat Satisfied"= "Satisfied","Satisfied"="Satisfied","Extremely Dissatisfied"="Dissatisfied"........etc, etc

我想使用地图功能来完成此任务,特别是at_map(维护数据框,但我是新手,purrr因此可以随意建议其他版本的地图)来自purrrtidyr stringr ggplot2` dplyr,因此一切都可以轻松流水线化.,and

下面的示例是我想要完成的,但要重新编码,但我无法使其工作。

http://www.r-bloggers.com/using-purrr-with-dplyr/

我想使用 at_map 或类似的 map 函数,这样我就可以保留 and 的原始列Sat1Sat2因此重新编码的列将被添加到数据框中并重命名。如果这个步骤也可以包含在一个函数中,那就太好了。

实际上,我会有很多数据帧,所以我只想重新编码一次因子水平,然后使用函数 frompurrr使用最少的代码对所有数据帧进行更改。

Names<-c("James","Chris","Jessica","Tomoki","Anna","Gerald")
Sat1<-c("Satisfied","Very Satisfied","Dissatisfied","Somewhat Satisfied","Dissatisfied","Neutral")
Sat2<-c("Very Dissatisfied","Somewhat Satisfied","Neutral","Neutral","Satisfied","Satisfied")
Program<-c("A","B","A","C","B","D")
Pets<-c("Snake","Dog","Dog","Dog","Cat","None")

DF1<-data.frame(Names,Sat1,Sat2,Program,Pets)

Names<-c("Tim","John","Amy","Alberto","Desrahi","Francesca")
Sat1<-c("Extremely Satisfied","Satisfied","Satisfed","Somewhat Dissatisfied","Dissatisfied","Satisfied")
Sat2<-c("Dissatisfied","Somewhat Dissatisfied","Neutral","Extremely Dissatisfied","Somewhat Satisfied","Somewhat Dissatisfied")
Program<-c("A","B","A","C","B","D")


DF2<-data.frame(Names,Sat1,Sat2,Program)
4

2 回答 2

1

做到这一点的一种方法是使用mutate_each结合其中一个map功能来完成数据帧列表的工作。使用dplyr_0.4.3.9001mutate_each或等效项允许您重命名新列。

在这种情况下,您可以使用字符串操作而不是重新编码。我相信您想从您拥有的当前字符串中提取Satisfied,Dissatisfied或。您可以使用正则表达式Neutral来实现这一点。sub例如,

sub(".*(Satisfied|Dissatisfied|Neutral).*$", "\\1", DF2$Sat2)
"Dissatisfied" "Dissatisfied" "Neutral"      "Dissatisfied" "Satisfied"    "Dissatisfied"

stringr有一个很好的功能来提取特定的字符串,str_extract.

library(stringr)
str_extract(DF2$Sat2, "Satisfied|Neutral|Dissatisfied")
 "Dissatisfied" "Dissatisfied" "Neutral"      "Dissatisfied" "Satisfied"    "Dissatisfied"

您可以在其中使用它mutate_each来在多个列上使用这些功能之一。您为其中的函数指定的名称funs将添加到新列名称中。我用过recode。对于您的一个数据集:

DF1 %>% 
    mutate_each( funs(recode = str_extract(., "Satisfied|Neutral|Dissatisfied") ), 
              starts_with("Sat") )

    Names               Sat1               Sat2 Program  Pets  Sat1_recode  Sat2_recode
1   James          Satisfied  Very Dissatisfied       A Snake    Satisfied Dissatisfied
2   Chris     Very Satisfied Somewhat Satisfied       B   Dog    Satisfied    Satisfied
3 Jessica       Dissatisfied            Neutral       A   Dog Dissatisfied      Neutral
4  Tomoki Somewhat Satisfied            Neutral       C   Dog    Satisfied      Neutral
5    Anna       Dissatisfied          Satisfied       B   Cat Dissatisfied    Satisfied
6  Gerald            Neutral          Satisfied       D  None      Neutral    Satisfied

要遍历存储在列表中的许多数据集,您可以使用purrrmap中的函数对列表中的每个元素执行函数。

list(DF1, DF2) %>%
    map(~mutate_each(.x, 
                  funs(recode = str_extract(., "Satisfied|Neutral|Dissatisfied") ), 
                  starts_with("Sat")) )

[[1]]
    Names               Sat1               Sat2 Program  Pets  Sat1_recode  Sat2_recode
1   James          Satisfied  Very Dissatisfied       A Snake    Satisfied Dissatisfied
2   Chris     Very Satisfied Somewhat Satisfied       B   Dog    Satisfied    Satisfied
...
[[2]]
      Names                  Sat1                   Sat2 Program  Sat1_recode  Sat2_recode
1       Tim   Extremely Satisfied           Dissatisfied       A    Satisfied Dissatisfied
2      John             Satisfied  Somewhat Dissatisfied       B    Satisfied Dissatisfied
...

相反,使用map_df会将列表中的所有元素绑定到 data.frame 中,这可能是也可能不是您想要的。使用该.id参数为每个原始数据集添加一个名称。

list(DF1, DF2) %>%
    map_df(~mutate_each(.x, 
                  funs(recode = str_extract(., "Satisfied|Neutral|Dissatisfied")), 
                  starts_with("Sat")), .id = "Group")

   Group     Names                  Sat1                   Sat2 Program  Pets  Sat1_recode
1      1     James             Satisfied      Very Dissatisfied       A Snake    Satisfied
2      1     Chris        Very Satisfied     Somewhat Satisfied       B   Dog    Satisfied
3      1   Jessica          Dissatisfied                Neutral       A   Dog Dissatisfied
4      1    Tomoki    Somewhat Satisfied                Neutral       C   Dog    Satisfied
5      1      Anna          Dissatisfied              Satisfied       B   Cat Dissatisfied
6      1    Gerald               Neutral              Satisfied       D  None      Neutral
7      2       Tim   Extremely Satisfied           Dissatisfied       A  <NA>    Satisfied
8      2      John             Satisfied  Somewhat Dissatisfied       B  <NA>    Satisfied
...
于 2016-06-22T14:33:59.433 回答
1

我用一个连接做像这样的大重新编码,在这种情况下,我认为转换为一个长数据帧可以让问题更容易思考。

library(tidyr)
library(dplyr)

mdf <- DF1 %>% 
  gather(var, value, starts_with("Sat"))

recode_df <- data_frame( value = c("Extremely Satisfied","Satisfied","Somewhat Dissatisfied","Dissatisfied"),
                         recode = 1:4)
mdf <- left_join(mdf, recode_df)
mdf %>% spread(var, recode)
于 2016-06-21T18:27:45.453 回答