{"id":107,"date":"2013-12-22T10:23:02","date_gmt":"2013-12-22T09:23:02","guid":{"rendered":"http:\/\/candrea.ch\/blog\/?p=107"},"modified":"2013-12-22T10:26:02","modified_gmt":"2013-12-22T09:26:02","slug":"compute-spss-like-mean-index-variables","status":"publish","type":"post","link":"https:\/\/candrea.ch\/blog\/compute-spss-like-mean-index-variables\/","title":{"rendered":"Compute SPSS like mean index variables"},"content":{"rendered":"<p>Suppose the problem discribed under\u00a0<a title=\"theanalysisfactor.com\" href=\"http:\/\/www.theanalysisfactor.com\/averaging-and-adding-variables-with-missing-data-in-spss\/\" target=\"_blank\">theanalysisfactor.com<\/a>.<\/p>\n<p>The point is to compute meaningfull mean index variables while missing values are present. In R you have the switch\u00a0<span class=\"lang:default decode:true  crayon-inline \">na.rm<\/span>\u00a0 to tell a function &#8211; here \u00a0the <span class=\"lang:default decode:true  crayon-inline \">mean()<\/span>\u00a0\u00a0function &#8211; \u00a0what to do with missing values. Setting this to true &#8211; <span class=\"lang:default decode:true  crayon-inline\">mean(&#8230;, na.rm=TRUE)<\/span>\u00a0\u00a0&#8211; forces the function to use all non-missing values, even if there is only one. In the other case &#8211; <span class=\"lang:default decode:true  crayon-inline\">mean(&#8230;, na.rm=FALSE)<\/span>\u00a0\u00a0&#8211; the function will return\u00a0<span class=\"lang:default decode:true  crayon-inline \">NA<\/span>\u00a0 even if there is ony one missing value.<\/p>\n<p>To handel this situation I have written a very handy function that works like the MEAN.{X}() function in SPSS, where {X} denotes the minimal number of variables that should be non-missing to be incorporated in computing the mean value.<\/p>\n<p>My single line R function looks like<\/p>\n<pre class=\"lang:r decode:true\">spss.row.means &lt;- function(vars, not.na=0) {\r\n  apply(vars, 1, function(x) ifelse(sum(!is.na(x)) &gt;= not.na, mean(x, na.rm=TRUE), NA))\r\n}<\/pre>\n<p>As the first argument you have to pass the variables (in columns) and the second argument is the minimal number of variables that should be non-missing.<\/p>\n<p>Have fun(ction)!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose the problem discribed under\u00a0theanalysisfactor.com. The point is to compute meaningfull mean index variables while missing values are present. In R you have the switch\u00a0na.rm\u00a0 to tell a function &#8211; here \u00a0the mean()\u00a0\u00a0function &#8211; \u00a0what to do with missing values. Setting this to true &#8211; mean(&#8230;, na.rm=TRUE)\u00a0\u00a0&#8211; forces the function to use all non-missing values, &hellip; <a href=\"https:\/\/candrea.ch\/blog\/compute-spss-like-mean-index-variables\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Compute SPSS like mean index variables<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":3,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}