Skip to content

Instantly share code, notes, and snippets.

@mbannert
Created June 17, 2017 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbannert/303233967b26195751f1ebc83f7912f7 to your computer and use it in GitHub Desktop.
Save mbannert/303233967b26195751f1ebc83f7912f7 to your computer and use it in GitHub Desktop.
computeScores <- function(dframe,
maplist,
fct = sum,
na.rm = T){
li <- lapply(maplist,function(x){
out <- dframe[,grep(x,names(dframe),value=T)]
apply(out,1,fct, na.rm = na.rm)
})
names(li) <- names(maplist)
cbind(dframe,as.data.frame(li))
}
out <- computeScores(big5_n,
list(A_sum = "^A",
E_sum = "^E",
N_sum = "^N",
O_sum = "^O",
C_sum = "^C"))
out <- computeScores(out,
list(A_mean = "^A[0-9]",
E_mean = "^E[0-9]",
N_mean = "^N[0-9]",
O_mean = "^O[0-9]",
C_mean = "^C[0-9]"),
fct = mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment