Reputation: 11
I'm very new to coding so I'm basically googling everything but I couldn't figure this one out:
I have a data frame of 32 rows, and 19 columns. I want to calculate the sum of each row in three specific columns.
I'm writing it like this:
D10
- my data frame.
Compliance_score
- the new column I want to add
Compliance_1-3
- the columns I want to sum
D10$Compliance_score = rowSums(D10[ ,c("Compliance_1", "Compliance_2", "Compliance_3"), drop = FALSE])
I keep getting the error: "incorrect number of dimensions".
Can't figure out what I'm doing wrong, or what this error message even means.
Any thoughts?
**editing: if I understood correctly what is a reproduce example (this is my first time, I hope I got this right- if not please let me know)
> dput(head(D10))
structure(list(PP = c("003", "014", "047", "013", "053", "048"
), MAAS_1 = c("4.0", "4.0", "3.0", "5.0", "3.0", "4.0"), MAAS_2 =
c("3.0",
"1.0", "6.0", "4.0", "3.0", "3.0"), MAAS_3 = c("4.0", "5.0",
"4.0", "3.0", "4.0", "4.0"), MAAS_4 = c("2.0", "2.0", "6.0",
"2.0", "3.0", "4.0"), MAAS_5 = c("3.0", "3.0", "4.0", "5.0",
"5.0", "5.0"), MAAS_6 = c("3.0", "3.0", "4.0", "3.0", "2.0",
"4.0"), MAAS_7 = c("3.0", "3.0", "4.0", "3.0", "3.0", "5.0"),
MAAS_8 = c("2.0", "4.0", "4.0", "4.0", "4.0", "4.0"), MAAS_9
= c("3.0",
"4.0", "3.0", "2.0", "4.0", "5.0"), MAAS_10 = c("3.0", "4.0",
"4.0", "2.0", "4.0", "4.0"), MAAS_11 = c("2.0", "5.0", "4.0",
"4.0", "1.0", "5.0"), MAAS_12 = c("2.0", "5.0", "6.0", "3.0",
"3.0", "6.0"), MAAS_13 = c("3.0", "3.0", "5.0", "3.0", "3.0",
"2.0"), MAAS_14 = c("3.0", "4.0", "5.0", "4.0", "4.0", "4.0"
), MAAS_15 = c("3.0", "5.0", "6.0", "3.0", "5.0", "5.0"),
Compliance_1 = c("0.0", "0.0", "0.0", "0.0", "1.0", "0.0"
), Compliance_2 = c("1.0", "0.0", "1.0", "0.0", "1.0", "0.0"
), Compliance_3 = c("0.0", "0.0", "0.0", "0.0", "0.0", "0.0"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"
))
>
Does that make sense?
Upvotes: 1
Views: 459
Reputation: 9250
Your problem is that your data is stored as a character, hence you need transform it to the class numeric
in order to calculate the sum, i.e.
library(dplyr)
New_D <- df %>%
mutate(across(starts_with("Compliance"), as.numeric)) %>%
mutate(Compliance_score = Compliance_1 + Compliance_2 + Compliance_3)
Or with @Claire suggestion when you have NA'values
New_D <- df %>%
mutate(across(starts_with("Compliance"), as.numeric)) %>%
mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3),
na.rm=TRUE))
Upvotes: 0
Reputation: 36
you can try this :
library(tidyverse)
New_D <- D10 %>%
mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3), na.rm=TRUE))
But a reproducible example would be great to understand the error.
Claire
Upvotes: 1