首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从R中给定的答案创建一组虚拟变量?

如何从R中给定的答案创建一组虚拟变量?
EN

Stack Overflow用户
提问于 2020-03-24 00:53:37
回答 3查看 33关注 0票数 0

我真的搞砸了这个问题。我有一个数据集:

代码语言:javascript
运行
复制
example = data.frame(age = c(34,19,44,22,34,12,54,63,23),
                       wash.hands = c("Before eating","Before eating, on public transportation","Before eating, After eating",
                                      "After eating","on public transportation, when I get home","Before eating",
                                      "When I get home","When I get home, Before eating","on public transportation"),
                     stringsAsFactors = F
                       )

看起来是这样的:

代码语言:javascript
运行
复制
# age                                wash.hands
#  34                             Before eating
#  19   Before eating, on public transportation
#  44               Before eating, After eating
#  22                              After eating
#  34 on public transportation, when I get home
#  12                             Before eating
#  54                           When I get home
#  63            When I get home, Before eating
#  23                  on public transportation

它包含了被调查者的年龄,以及他何时洗手。我希望有一组4个虚拟变量(吃饭前,吃完饭后,乘坐公共交通工具,当我回到家时),如果受访者在特定场合洗手,则将它们签名为"1“,否则签名为0。我该怎么做?任何帮助都将不胜感激!谢谢!:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-03-24 01:38:40

这是一种基于R的方法。

代码语言:javascript
运行
复制
times <- c("Before eating","on public transportation","After eating","When I get home")
result <- lapply(times,function(x){as.numeric(grepl(x,example$wash.hands))})
names(result) <- times
cbind(example,do.call(cbind,result))
  age                                wash.hands Before eating on public transportation After eating When I get home
1  34                             Before eating             1                        0            0               0
2  19   Before eating, on public transportation             1                        1            0               0
3  44               Before eating, After eating             1                        0            1               0
4  22                              After eating             0                        0            1               0
5  34 on public transportation, when I get home             0                        1            0               0
6  12                             Before eating             1                        0            0               0
7  54                           When I get home             0                        0            0               1
8  63            When I get home, Before eating             1                        0            0               1
9  23                  on public transportation             0                        1            0               0
票数 0
EN

Stack Overflow用户

发布于 2020-03-24 00:58:42

我将使用str_detect()来指示变量中是否有一组特定的字符串。

代码语言:javascript
运行
复制
library(tidyverse)

mutate(example,
  before_eating = str_detect(wash.hands, "Before eating"),
  after_eating = str_detect(wash.hands, "After eating"),
  public_trans = str_detect(wash.hands, "public transportation"),
  get_home = str_detect(wash.hands, "get home"))

这将返回4个布尔变量,R将TRUE视为1,将FALSE视为0,因此这应该适用于您要对其执行的任何分析。

票数 0
EN

Stack Overflow用户

发布于 2020-03-24 01:32:27

可以使用psychfastDummies

代码语言:javascript
运行
复制
library(psych)
dummy.code(example$wash.hands)

library(fastDummies)
dummy_cols(example$wash.hands)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60818049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档