Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sjmisc::rec() behaviour with mutate() and value ranges #131

Open
konstruktur opened this issue Feb 12, 2020 · 0 comments
Open

sjmisc::rec() behaviour with mutate() and value ranges #131

konstruktur opened this issue Feb 12, 2020 · 0 comments

Comments

@konstruktur
Copy link

konstruktur commented Feb 12, 2020

Hi,
I'd like to use sjmisc::rec() with value ranges within mutate(). I just found some of my written code that I think worked some months ago with sjmisc::rec(). But I'm now not completely sure whether this worked or not, at least I find the result, using rec() in mutate() with/without value ranges somehow inconsistent - but I think you can tell a little better whether this should work or not ;-) I prepared some examples. I use sjmisc v2.8.3 on R 3.6.1.

df.rec <- structure(list(x = c(2400, 1538.46153846154, 2500, 2638.88888888889, 
                     2083.33333333333, 5555.55555555556, 2666.66666666667, 2500, 2833.33333333333, 
                     2500, NA, 1750, 2833.33333333333, NA, 600, 1785.71428571429, 
                     2000, 2400, 1333.33333333333, 1333.33333333333)), row.names = c(NA, -20L), class = c("rowwise_df", "tbl_df", "tbl", "data.frame"))


### rec() without pipes # Works! Returns vector
sjmisc::rec(df.rec$x, rec="0:1258.99 = 1[below 1259]; 1259:max = 0[above 1259]; else=NA", var.label = "sjmisc::rec() test1")  


### rec() with pipes # Works! Returns df
df.rec %>% sjmisc::rec(x, rec="0:1258.99 = 1[below 1259]; 1259:max = 0[above 1259]; else=NA", var.label = "sjmisc::rec() test2")  


### rec() within mutate # Does not work: Error " 'to' must be a finite number"
df.rec %>% mutate(y = sjmisc::rec(x, rec="0:1258.99 = 1[below 1259]; 1259:max = 0[above 1259]; else=NA", var.label = "sjmisc::rec() test3"))  
#Error in seq.default(from, to) : 'to' must be a finite number
#In addition: Warning messages:
#1: In min(x, na.rm = T) : no non-missing arguments to min; returning Inf
#2: In max(x, na.rm = T) : no non-missing arguments to max; returning -Inf


### rec() within mutate, but without value ranges # Works with warnings
# Here I take out the spans and just recode a single value and copy the rest - and this works.
df.rec %>% mutate(y = sjmisc::rec(x, rec="2400 = 1[just 2400]; else=copy", var.label = "sjmisc::rec() test4"))  
#Warning messages:
# 1: In min(x, na.rm = T) : no non-missing arguments to min; returning Inf
#2: In max(x, na.rm = T) : no non-missing arguments to max; returning -Inf
#3: In min(x, na.rm = T) : no non-missing arguments to min; returning Inf
#4: In max(x, na.rm = T) : no non-missing arguments to max; returning -Inf

The latter works (with warnings). So I'm a bit confused whether my use with mutate() causes the above errors or the value ranges (e.g. 0:1258.99) I try to recode with.

Thanks fo any hint!
Best, G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant