在R中创建一个具有独家条件的变量

发布于 2025-01-31 10:16:33 字数 496 浏览 4 评论 0 原文

我的数据看起来像这样

Q3A1<-c(0,1,0,1,1,1,0,1,0,1)
Q3A2<-c(0,1,1,1,0,1,0,0,0,1)
Q3A3<-c(1,1,0,1,0,1,0,0,0,1)

,我想创建一个新的变量Q3L,如果将Q1A1,Q1A2和Q1A3编码为1,

我尝试过的是1

dataQ$Q3L <- ifelse(dataQ$Q3A1==1|dataQ$Q3A2==1|dataQ$Q3A3==1, 1, 0 )

,但它似乎是在重新编码,因为至少三个等于1,但是我需要它们的树等于1,

我尝试了这一点,

library(dplyr)
dataQ %>%
  mutate(Q3L = case_when(Q3A1 == 1 & Q3A2 == 1 & Q3A3 == 1 ~ 1,))

也不是成功

My data look like this

Q3A1<-c(0,1,0,1,1,1,0,1,0,1)
Q3A2<-c(0,1,1,1,0,1,0,0,0,1)
Q3A3<-c(1,1,0,1,0,1,0,0,0,1)

And I would like to create a new variable Q3L were it is coded as 1 when Q1A1, Q1A2 and Q1A3 are all equal to 1

I tried this

dataQ$Q3L <- ifelse(dataQ$Q3A1==1|dataQ$Q3A2==1|dataQ$Q3A3==1, 1, 0 )

but It seemslike it is recoding as 1 if at least one of the three equal 1, but I need the tree of them to be equal to 1

I tried this as well

library(dplyr)
dataQ %>%
  mutate(Q3L = case_when(Q3A1 == 1 & Q3A2 == 1 & Q3A3 == 1 ~ 1,))

not a success neither

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漫漫岁月 2025-02-07 10:16:33

我们可以在列名中使用 if_all - 在 start_with 'q3a'的列中循环,创建逻辑条件以检查列值是否为1()。 x == 1 ), if_all 返回 true 仅在一行的所有列都是true的情况下,即全部1,然后强迫逻辑(> )是/false ),用+ as.integer

library(dplyr)
dataQ <- dataQ %>% 
   mutate(Q3L = +(if_all(starts_with('Q3A'), ~ .x== 1)))

注意:使用 | &amp;

>在可变数量的列数上可能很麻烦,而是使用 if_all 或使用 rowsums data

dataQ <- data.frame(Q3A1, Q3A2, Q3A3)

We can use if_all - by looping over the columns that starts_with 'Q3A' in column names, create a logical condition to check whether the column values are 1 (.x == 1), if_all returns TRUE only when all of the columns for a row are TRUE i.e. all 1, then coerce the logical (TRUE/FALSE) to binary with + or as.integer

library(dplyr)
dataQ <- dataQ %>% 
   mutate(Q3L = +(if_all(starts_with('Q3A'), ~ .x== 1)))

NOTE: Using | or & on a variable number of columns can be cumbersome, instead using either if_all or create the logical with rowSums

data

dataQ <- data.frame(Q3A1, Q3A2, Q3A3)
作死小能手 2025-02-07 10:16:33

更新:感谢@LMC的输入:一种更具概括的方法是:

library(dplyr)

tibble(Q3A1, Q3A2, Q3A3) %>% 
  mutate(Q3L = ifelse(select(., contains("Q3")) %>% 
                        rowSums() == ncol(.), 1, 0))

第一个答案:

我们可以使用 rowsums 并应用 ifelse 语句:

library(dplyr)

tibble(Q3A1, Q3A2, Q3A3) %>% 
  mutate(Q3L = ifelse(select(., contains("Q3")) %>% 
                        rowSums() == 3, 1, 0))
    Q3A1  Q3A2  Q3A3   Q3L
   <dbl> <dbl> <dbl> <dbl>
 1     0     0     1     0
 2     1     1     1     1
 3     0     1     0     0
 4     1     1     1     1
 5     1     0     0     0
 6     1     1     1     1
 7     0     0     0     0
 8     1     0     0     0
 9     0     0     0     0
10     1     1     1     1

Update: thanks to input from @LMc: a more generalizable way would be:

library(dplyr)

tibble(Q3A1, Q3A2, Q3A3) %>% 
  mutate(Q3L = ifelse(select(., contains("Q3")) %>% 
                        rowSums() == ncol(.), 1, 0))

First answer:

We could check with rowSums and apply an ifelse statement:

library(dplyr)

tibble(Q3A1, Q3A2, Q3A3) %>% 
  mutate(Q3L = ifelse(select(., contains("Q3")) %>% 
                        rowSums() == 3, 1, 0))
    Q3A1  Q3A2  Q3A3   Q3L
   <dbl> <dbl> <dbl> <dbl>
 1     0     0     1     0
 2     1     1     1     1
 3     0     1     0     0
 4     1     1     1     1
 5     1     0     0     0
 6     1     1     1     1
 7     0     0     0     0
 8     1     0     0     0
 9     0     0     0     0
10     1     1     1     1
梦里南柯 2025-02-07 10:16:33

您可以这样做:

dataQ["Q3L"] = +(dataQ$Q3A1 & dataQ$Q3A2 & dataQ$Q3A3)

输出:

   Q3A1 Q3A2 Q3A3 Q3L
1     0    0    1   0
2     1    1    1   1
3     0    1    0   0
4     1    1    1   1
5     1    0    0   0
6     1    1    1   1
7     0    0    0   0
8     1    0    0   0
9     0    0    0   0
10    1    1    1   1

You can do this:

dataQ["Q3L"] = +(dataQ$Q3A1 & dataQ$Q3A2 & dataQ$Q3A3)

Output:

   Q3A1 Q3A2 Q3A3 Q3L
1     0    0    1   0
2     1    1    1   1
3     0    1    0   0
4     1    1    1   1
5     1    0    0   0
6     1    1    1   1
7     0    0    0   0
8     1    0    0   0
9     0    0    0   0
10    1    1    1   1
梦断已成空 2025-02-07 10:16:33
library(dplyr)

df %>% 
  rowwise() %>% 
  mutate(Q3L = as.numeric(all(as.logical(c_across(starts_with("Q3")))))) %>% 
  ungroup()

或使用 purrr :: pmap_dbl

library(purrr)
library(dplyr)

df %>% 
  mutate(Q3L = pmap_dbl(across(starts_with("Q3")), ~ all(.x == 1)))

purrr ::降低

df %>% 
  mutate(Q3L = as.numeric(reduce(across(starts_with("Q3")), `&`)))

utput> output>

    Q3A1  Q3A2  Q3A3   Q3L
   <dbl> <dbl> <dbl> <dbl>
 1     0     0     1     0
 2     1     1     1     1
 3     0     1     0     0
 4     1     1     1     1
 5     1     0     0     0
 6     1     1     1     1
 7     0     0     0     0
 8     1     0     0     0
 9     0     0     0     0
10     1     1     1     1

它的工作原理

两个答案都同样有效。他们逐行,将行转换为逻辑向量。 所有然后测试行逻辑向量的每个元素是 true 。如果是 1 将返回,否则 0

purrr :: dred 的工作方式有所不同。对于每一行,它都使用&amp; 函数将行矢量降低到单个逻辑值中。

library(dplyr)

df %>% 
  rowwise() %>% 
  mutate(Q3L = as.numeric(all(as.logical(c_across(starts_with("Q3")))))) %>% 
  ungroup()

Or with purrr::pmap_dbl

library(purrr)
library(dplyr)

df %>% 
  mutate(Q3L = pmap_dbl(across(starts_with("Q3")), ~ all(.x == 1)))

Or purrr::reduce

df %>% 
  mutate(Q3L = as.numeric(reduce(across(starts_with("Q3")), `&`)))

Output

    Q3A1  Q3A2  Q3A3   Q3L
   <dbl> <dbl> <dbl> <dbl>
 1     0     0     1     0
 2     1     1     1     1
 3     0     1     0     0
 4     1     1     1     1
 5     1     0     0     0
 6     1     1     1     1
 7     0     0     0     0
 8     1     0     0     0
 9     0     0     0     0
10     1     1     1     1

How it works

Both answers work similarly. They go row-by-row and convert the rows to logical vectors. all then tests if every element of the row-wise logical vector is TRUE. If it is then 1 is returned otherwise 0.

purrr::reduce works a bit differently. For each row it reduces the row-wise vector using the & function into a single logical value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文