I want to create a new variable, "F", by adding columns (B C D E) if the column "A" is 1.
ID | A | B | C | D | E |
---|---|---|---|---|---|
001 | 1 | 1 | 2 | NA | 1 |
002 | 0 | 2 | 1 | 1 | NA |
df$F <- rowSums(df[df$A == '1', c(3:6)],na.rm=TRUE)
I get this error:
Error:
! Assigned data `rowSums(df[df$A == "1", c(3:6)], na.rm = TRUE)` must be compatible with existing data.
✖ Existing data has 12358 rows.
✖ Assigned data has 474 rows.
ℹ Only vectors of size 1 are recycled.
Backtrace:
1. base::`$<-`(`*tmp*`, F, value = `<dbl>`)
12. tibble (local) `<fn>`(`<vctrs___>`)
Error:
How can I fix this? Are there other ways to get my final outcome something looks like the one below?
ID | A | B | C | D | E | F |
---|---|---|---|---|---|---|
001 | 1 | 1 | 2 | NA | 1 | 4 |
002 | 0 | 2 | 1 | 1 | NA | NA |
CodePudding user response:
Try this.
df$F <- ifelse(df$A %in% 1, rowSums(df[, c("B", "C", "D", "E")], na.rm=TRUE), NA)
df
# ID A B C D E F
# 1 1 1 1 2 NA 1 4
# 2 2 0 2 1 1 NA NA
Note, that due to using %in%
, NA
s in column A
are treated like 0
.
CodePudding user response:
We just need the logical to be on the lhs as well to keep the lengths same
df$F[df$A == '1'] <- rowSums(df[df$A == '1', c(3:6)],na.rm=TRUE)
-output
> df
ID A B C D E F
1 1 1 1 2 NA 1 4
2 2 0 2 1 1 NA NA
CodePudding user response:
A tidyverse
approach:
Libraries
library(dplyr)
Data
data <-
tibble::tribble(
~ID, ~A, ~B, ~C, ~D, ~E,
"001", 1L, 1L, 2L, NA, 1L,
"002", 0L, 2L, 1L, 1L, NA
)
Code
data %>%
rowwise() %>%
mutate(`F` = if_else(A == 1, sum(c_across(cols = B:E),na.rm = TRUE), NA_integer_) )
Output
# A tibble: 2 x 7
# Rowwise:
ID A B C D E F
<chr> <int> <int> <int> <int> <int> <int>
1 001 1 1 2 NA 1 4
2 002 0 2 1 1 NA NA