psychbruce / bruceR

📦 BRoadly Useful Convenient and Efficient R functions that BRing Users Concise and Elegant R data analyses.
https://psychbruce.github.io/bruceR/
GNU General Public License v3.0
167 stars 36 forks source link

Alpha function #8

Closed jasonmoy28 closed 3 years ago

jasonmoy28 commented 3 years ago

Based on the documentation, I think the Alpha function required the item to be in the format of common var name + unique var name (e.g., a1 , a2, a3 should be written as var = a, items = 1:3). However, in practice, that's not always the case to have a common var name follow by a unique var name. For example, I want to use the item Q67, Q88, Q89a, Q89b, Q89c, and it is not supported by the Alpha function (at least I don't know how to do it by reading the documentation)

I think it would be much more useful if the function supports subsetting using the dplyr::select() syntax and the helpers (e.g., everything(), starts_with()).

psychbruce commented 3 years ago

Thanks. Your request CAN be supported by the Alpha() function. As shown in the documentation, two options can be used to specify the variables. Option 1 is using the combination of var and items. Option 2 is using the parameter vars. Here I show how it works.

Suppose you want to select variables Q67, Q88, Q89a, Q89b, Q89c, the most direct way is:

Alpha(data, vars=c("Q67", "Q88", "Q89a", "Q89b", "Q89c"))

and the parameter rev (if necessary) should also be a character vector (e.g., rev="Q88" or rev=c("Q67", "Q88")).

Or if you want to use dplyr::select(), just use it in the parameter vars:

Alpha(data, vars=names(select(data, Q67, Q88, Q89a, Q89b, Q89c)))

or using %>% if you want:

data %>% select(Q67, Q88, Q89a, Q89b, Q89c) %>% Alpha(vars=names(.))

In this way, the selection helpers you mentioned (e.g., everything(), starts_with()) can also be used.


NEWS

In the forthcoming version 0.6.1, Alpha() will add a parameter varrange (to keep the same as SUM(), MEAN(), ...) and report both Cronbach's α and McDonald's ω, with more detailed documentation.

Three ways to specify the variable list:

  1. var + items: use the common and unique parts of variable names.
  2. vars: directly define the variable list.
  3. varrange: use the start and end positions of the variable list.

Best, Bruce