我真的搞砸了这个问题。我有一个数据集:
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
                       )看起来是这样的:
# 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。我该怎么做?任何帮助都将不胜感激!谢谢!:)
发布于 2020-03-24 01:38:40
这是一种基于R的方法。
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发布于 2020-03-24 00:58:42
我将使用str_detect()来指示变量中是否有一组特定的字符串。
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,因此这应该适用于您要对其执行的任何分析。
发布于 2020-03-24 01:32:27
可以使用psych或fastDummies
library(psych)
dummy.code(example$wash.hands)
library(fastDummies)
dummy_cols(example$wash.hands)https://stackoverflow.com/questions/60818049
复制相似问题