Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将列拆分为多个列,并根据其是否存在存储0或1_R - Fatal编程技术网

R 将列拆分为多个列,并根据其是否存在存储0或1

R 将列拆分为多个列,并根据其是否存在存储0或1,r,R,因此,我目前有一个用户ID和药物类型的数据集,我正在尝试格式化该数据集,以便将所有药物设置为列,如果用户服用这些药物,它将有一个1,如果不是0。我不确定实现这一点的最佳方式,我已经尝试了spread,但它不喜欢数据集,因为用户ID不是唯一的 这就是我试图创造的: #starting df df1 <- data.frame (user_id =c("1", "1", "2", "3","3"

因此,我目前有一个用户ID和药物类型的数据集,我正在尝试格式化该数据集,以便将所有药物设置为列,如果用户服用这些药物,它将有一个1,如果不是0。我不确定实现这一点的最佳方式,我已经尝试了spread,但它不喜欢数据集,因为用户ID不是唯一的

这就是我试图创造的:

#starting df 
df1 <- data.frame (user_id =c("1", "1", "2", "3","3"),
                   medication_name =c("Carbamzepine", "Clonazepam", "Lamotrigine", "Zonisamide", "Gabapentin"))

#The df im trying to make

df2 <- data.frame(user_id = c("1", "2", "3"),
                  Carbamzepine = c("1","0","0"),
                  Clonazepam = c("1", "0","0"),
                  Lamotrigine = c("0","1","0"),
                  Zonisamide = c("0", "0", "1"),
                  Gabapentin = c("0", "0", "1"))
使用表:

table(df1)
给予:

       medication_name
user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
      1            1          1          0           0          0
      2            0          0          0           1          0
      3            0          0          1           0          1
  user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
1       1            1          1          0           0          0
2       2            0          0          0           1          0
3       3            0          0          1           0          1
这将提供一个表对象,但如果它必须是数据帧,则将其转换为数据帧,然后将行名称移动到用户id列

library(tibble)
rownames_to_column(as.data.frame.matrix(table(df1)), "user_id")
给予:

       medication_name
user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
      1            1          1          0           0          0
      2            0          0          0           1          0
      3            0          0          1           0          1
  user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
1       1            1          1          0           0          0
2       2            0          0          0           1          0
3       3            0          0          1           0          1
使用表:

table(df1)
给予:

       medication_name
user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
      1            1          1          0           0          0
      2            0          0          0           1          0
      3            0          0          1           0          1
  user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
1       1            1          1          0           0          0
2       2            0          0          0           1          0
3       3            0          0          1           0          1
这将提供一个表对象,但如果它必须是数据帧,则将其转换为数据帧,然后将行名称移动到用户id列

library(tibble)
rownames_to_column(as.data.frame.matrix(table(df1)), "user_id")
给予:

       medication_name
user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
      1            1          1          0           0          0
      2            0          0          0           1          0
      3            0          0          1           0          1
  user_id Carbamzepine Clonazepam Gabapentin Lamotrigine Zonisamide
1       1            1          1          0           0          0
2       2            0          0          0           1          0
3       3            0          0          1           0          1

您可以使用以下解决方案:

library(dplyr)
library(tidyr)

df %>%
  mutate(id = 1) %>%
  pivot_wider(names_from = medication_name, values_from = id, 
              values_fill = 0)


# A tibble: 3 x 6
  user_id Carbamzepine Clonazepam Lamotrigine Zonisamide Gabapentin
  <chr>          <dbl>      <dbl>       <dbl>      <dbl>      <dbl>
1 1                  1          1           0          0          0
2 2                  0          0           1          0          0
3 3                  0          0           0          1          1

您可以使用以下解决方案:

library(dplyr)
library(tidyr)

df %>%
  mutate(id = 1) %>%
  pivot_wider(names_from = medication_name, values_from = id, 
              values_fill = 0)


# A tibble: 3 x 6
  user_id Carbamzepine Clonazepam Lamotrigine Zonisamide Gabapentin
  <chr>          <dbl>      <dbl>       <dbl>      <dbl>      <dbl>
1 1                  1          1           0          0          0
2 2                  0          0           1          0          0
3 3                  0          0           0          1          1

这太棒了!如果用户多次使用该中介,我如何确保它保持为1,而不是变为2或更高?如果您想消除重复项,那么TableUniqued1这非常有效!如果用户多次使用该中介,如何确保它保持为1,而不是变为2或更高?如果要消除重复项,请使用tableuniquedf1