'incorrect number of dimensions R - rowSums
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?
Solution 1:[1]
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
Solution 2:[2]
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))
Solution 3:[3]
You are on a right way, but you are not using Array.filter properly.
And also the provided array of object is not also in a good format. Object should be in a key:value pairs.
state.products = [
{ id: 1, name: "Bottle" },
{ id: 2, name: "umbrella" },
{ id: 3, name: "shoe" }
]
const getAllArrayExcept = (id) => {
// this will return all the array except provided id
return state.products.filter((s) => s.id !== id)
}
const getOnlyArray = (id) => {
// this will return only item which match the provided id
return state.products.filter((s) => s.id === id)
}
console.log(getAllArrayExcept(1))
/*
output: [{ id = 2, name = umbrella }, { id = 3, name = shoe }]
*/
console.log(getOnlyArray(1))
/*
output: [{ id = 1, name = Bottle }]
*/
Here is the working snippet:
const products = [
{ id: 1, name: "Bottle" },
{ id: 2, name: "umbrella" },
{ id: 3, name: "shoe" }
]
const getAllArrayExcept = (id) => {
// this will return all the array except provided id
return products.filter((s) => s.id !== id)
}
const getOnlyArray = (id) => {
// this will return only item which match the provided id
return products.filter((s) => s.id === id)
}
console.log("All array except: ", getAllArrayExcept(1))
console.log("Only provided item in array: ", getOnlyArray(1))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Claire Kermorvant |
| Solution 2 | Julian |
| Solution 3 | Pradip Dhakal |
