0

I have a large dataset and super new to R. I am trying to sum across each row for columns 226-245 (These are not the names for the columns, just positions that the columns are in).

Some of my rows contain a few NA values, but I still want to calculate the numbers around those NA values, so that I don't get any NA's in the output. (So even if a row contains NA values in its columns, I want to calculate the values that aren't NA in the row so that I dont get NA as an output). Here is what I have so far, but it is still returning NA values in my sum row. For example, If the values across my row are : 1, 2, NA, 3. I still want to get a value of 6 in my output, but I am getting NA's.

CONFUSION$row_sum = rowSums(CONFUSION[,c(226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,na.rm=TRUE)])
  • (The backticks weren't in my original code, I accidentally made a mistake typing them here) – Cassidy Tomlinson Jan 21 '22 at 05:01
  • You are missing a right paren `)` to close out `mutate(`. FYI, code-fences use the three backticks `\`\`\`` to start and end the block, and they must be on lines by themselves, not immediately before or after code; see https://stackoverflow.com/editing-help. – r2evans Jan 21 '22 at 05:04
  • Please provide sample data and expected output, see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 21 '22 at 05:07

1 Answers1

0

I'll demo using mtcars and columns 3 to 5,

mtcars[1:3,4] <- NA
head(mtcars)
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4         21.0   6  160  NA 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag     21.0   6  160  NA 3.90 2.875 17.02  0  1    4    4
# Datsun 710        22.8   4  108  NA 3.85 2.320 18.61  1  1    4    1
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

dplyr

library(dplyr)
mtcars %>%
  mutate(rowsums = rowSums(cur_data()[3:5], na.rm = TRUE)) %>%
  head()
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb rowsums
# Mazda RX4         21.0   6  160  NA 3.90 2.620 16.46  0  1    4    4  163.90
# Mazda RX4 Wag     21.0   6  160  NA 3.90 2.875 17.02  0  1    4    4  163.90
# Datsun 710        22.8   4  108  NA 3.85 2.320 18.61  1  1    4    1  111.85
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1  371.08
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  538.15
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  332.76

base R

mtcars$rowsum <- rowSums(mtcars[3:5], na.rm = TRUE)
head(mtcars)
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb rowsum
# Mazda RX4         21.0   6  160  NA 3.90 2.620 16.46  0  1    4    4 163.90
# Mazda RX4 Wag     21.0   6  160  NA 3.90 2.875 17.02  0  1    4    4 163.90
# Datsun 710        22.8   4  108  NA 3.85 2.320 18.61  1  1    4    1 111.85
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 371.08
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 538.15
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 332.76
r2evans
  • 141,215
  • 6
  • 77
  • 149