Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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 使用mutate_at使用数据帧中的多个其他列创建新列_R - Fatal编程技术网

R 使用mutate_at使用数据帧中的多个其他列创建新列

R 使用mutate_at使用数据帧中的多个其他列创建新列,r,R,假设我有这样一个数据帧: x <- data.frame(d1 = c(1,0,1,3,5), d2 = c(0,0,1,1,0)) # d1 d2 # 1 1 0 # 2 0 0 # 3 1 1 # 4 3 1 # 5 5 0 使用dplyr,我可以通过首先使用filter_分割数据帧,然后再将结果数据帧绑定在一起来实现这一点 x1 <- x %>% filter_at(vars(starts_with("d")), any_vars(. ==

假设我有这样一个数据帧:

x <- data.frame(d1 = c(1,0,1,3,5), d2 = c(0,0,1,1,0))

#   d1 d2
# 1  1  0
# 2  0  0
# 3  1  1
# 4  3  1
# 5  5  0
使用dplyr,我可以通过首先使用filter_分割数据帧,然后再将结果数据帧绑定在一起来实现这一点

x1 <- x %>%
  filter_at(vars(starts_with("d")), any_vars(. == 1))
x1$R <- 1

x2 <- x %>%
  filter_at(vars(starts_with("d")), all_vars(. != 1))
x2$R <- 0

x <- bind_rows(x1,x2)

#   d1 d2 R
# 1  1  0 1
# 2  1  1 1
# 3  3  1 1
# 4  0  0 0
# 5  5  0 0

然而,这种方法似乎不仅是完成此任务的一种迂回方式,而且数据帧顺序也发生了变化。在我的情况下这不是一个问题,但在其他情况下可能会令人讨厌。有人知道更好的方法吗?

如果我正确理解了你的问题,我会在ifelse语句中使用mutate函数

x <- data.frame(d1 = c(1,0,1,3,5), d2 = c(0,0,1,1,0))

# the | is a logical OR operator
x %>% mutate(R = ifelse(d1 ==1 | d2== 1,1,0))

   d1 d2 R
 1  1  0 1
 2  0  0 0
 3  1  1 1
 4  3  1 1
 5  5  0 0

如果我正确理解了你的问题,我会使用带有ifelse语句的mutate函数

x <- data.frame(d1 = c(1,0,1,3,5), d2 = c(0,0,1,1,0))

# the | is a logical OR operator
x %>% mutate(R = ifelse(d1 ==1 | d2== 1,1,0))

   d1 d2 R
 1  1  0 1
 2  0  0 0
 3  1  1 1
 4  3  1 1
 5  5  0 0
在此处了解有关ifelse的更多信息:

使用data.table包尝试此操作:

基准R:

x$result <- ifelse(x$d1==1 | x$d2==1,1,0)
在此处了解有关ifelse的更多信息:

使用data.table包尝试此操作:

基准R:

x$result <- ifelse(x$d1==1 | x$d2==1,1,0)

如果不确定将有多少个d列,可以先重塑形状

library(dplyr)
library(tidyr)
x %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(starts_with('d')) %>% 
  group_by(id) %>% 
  mutate(R = as.numeric(any(value == 1))) %>% 
  pivot_wider()

如果不确定将有多少个d列,可以先重塑形状

library(dplyr)
library(tidyr)
x %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(starts_with('d')) %>% 
  group_by(id) %>% 
  mutate(R = as.numeric(any(value == 1))) %>% 
  pivot_wider()

另一个选择是:

x %>%
 mutate(R = +(rowSums(select(., starts_with("d")) == 1) != 0))

  d1 d2 R
1  1  0 1
2  0  0 0
3  1  1 1
4  3  1 1
5  5  0 0

另一个选择是:

x %>%
 mutate(R = +(rowSums(select(., starts_with("d")) == 1) != 0))

  d1 d2 R
1  1  0 1
2  0  0 0
3  1  1 1
4  3  1 1
5  5  0 0

如果以d开头的列数不同,则在以R为底的列中,可以执行以下操作:

cbind(x, R = +Reduce("|",data.frame(x[startsWith(names(x),"d")]==1)))
  d1 d2 R
1  1  0 1
2  0  0 0
3  1  1 1
4  3  1 1
5  5  0 0

如果以d开头的列数不同,则在以R为底的列中,可以执行以下操作:

cbind(x, R = +Reduce("|",data.frame(x[startsWith(names(x),"d")]==1)))
  d1 d2 R
1  1  0 1
2  0  0 0
3  1  1 1
4  3  1 1
5  5  0 0
mutatex,R=as.numericd1==1 | d2==1?这听起来像是一个ifelse可能有用的作业,可以在mutate.mutatex中使用,R=as.numericd1==1 | d2==1?这听起来像是一个ifelse可能有用的作业,可以在mutate中使用。