I'm very new with the R language, apologies for the noob question.
I'm trying to find a correlation between sex and total Usability (totalU), total Satisfaction (totalS), and total Ease of Use (totalE). Also, sexNumeric is basically Male = 1, Female = 2, vice versa.
For efficiency purposes, I'd like to learn about how you can loop this. I've tried doing:
x <- sexNumeric
z <- list(totalU,totalS,totalE)
for (i in z){
cor(x,z)
}
But it does not work, it says "Error in cor(x,z) : 'y' must be numeric."
Here is the link of the csv file: https://drive.google.com/file/d/1MlmaLGFpm94dLssMAX6oFj0_P5chuUJi/view?usp=sharing
Here is a reproducible sample:
dat <- read.csv(file = "Canva_ApplicationUsability.csv", header = TRUE)
totalU = rowSums(dat[,c(4:8)],na.rm=TRUE) #Get the sum of each respondent in Usefulness
totalS = rowSums(dat[,c(9:13)],na.rm=TRUE) #Get the sum of each respondent in Satisfaction
totalE = rowSums(dat[,c(14:18)],na.rm=TRUE) #Get the sum of each respondent in Ease of Use
#Assign Numeric Values to Characters of Sex
sexNumeric <- dat[1]
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Male", 1))
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Female", 2))
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Transgender Male", 3))
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Transgender Female", 4))
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Gender Variant/Non-Conforming", 5))
sexNumeric <- sexNumeric %>% mutate(Sex = replace(Sex, Sex == "Prefer Not to Answer", 6))
sexNumeric$Sex <- as.numeric(sexNumeric$Sex)
So yeah, those are the where sexNumeric and totalU, totalS, and totalE come from. Then, I'd like to find the correlation between sexNumeric (x) and totalU, totalS, and totalE (y) in one loop.
Thanks in advance!
Edit 1: Specifically, for every loop, I want z to be replaced by totalU, then in the next loop replaced by totalS, then in the next by totalE. Each of those variables contain values ranging from 1 to 5, it's from a Likert-scale survey.
CodePudding user response:
According to ?cor
, x
can be a vector, matrix or data.frame, similarly, if we provide y
, it can be as well. From the list
of vector
s, we can cbind
the elements to create to a single matrix and use that in cor
cor(x, do.call(cbind, z))
the do.call
with cbind
does the same as
cbind(z[[1]], z[[2]], z[[3]])
in a compact way i.e if there are 100s of list elements, we don't have to extract each element separately for cbind
ing
CodePudding user response:
You can provide more than one numeric
variable to cor()
, it will return a correlation matrix:
data("trees") #build-in dataset
cor(trees)
Girth Height Volume
Girth 1.0000000 0.5192801 0.9671194
Height 0.5192801 1.0000000 0.5982497
Volume 0.9671194 0.5982497 1.0000000