Hi, I have my data frame with multiple int variables (B1, C1, D1 etc) and they contain numbers from 1 to 10 (and NAs). Is any way of multiplying all of the responses by 10 in one go? 1s should become 10s, 2s-20s...10s-100s.
I also need to recode all of these int variables into new three level variables (B13L, C13L, D13L) where 1-6 (or 10 to 60 after recoding) =1, 7-8 (or 70-80)=2 and 9=10 (90-100)=3.
The mutate_if() and mutate_at() functions—part of dplyr—can help you here! They both allow you to modify a number of columns in the same way, but they allow you to choose the target columns differently: mutate_if() lets you target columns that pass a test (called a predicate function), while mutate_at() lets you specify the columns (based on the their name) using a collection of helper functions. For example:
# mutate all the numeric functions
df %>% mutate_if(is.numeric, ~ .x * 10)
# mutate all the columns whose names end in '3L'
df %>% mutate_at(vars(ends_with('3L')), ~ .x * 10)
Thank you very much. That is an excellent solution. I have tried that for B1, C1 etc. using
df %>% mutate_at(vars(ends_with('1')), ~ .x * 10)
and it worked (king of) but results were not saved. I found it because I realised that one variable ending with 2 did not change:
A1 A2 B1 C1
90 9 90 90
I used this code:
df %>% mutate_at(vars(ends_with('2')), ~ .x * 10)
and I can see that only this variable changed this time but others are unchanged:
A1 A2 B1 C1
9 90 9 9
How can I fix it? I need all of them to be recoded and saved in the data frame.
I also need to recode all of these int variables into new three level variables (B13L, C13L, D13L) where 1-6 (or 10 to 60 after recoding) =1, 7-8 (or 70-80)=2 and 9=10 (90-100)=3.
A sidenote here. I see you trying to create reprexes all around the site, but none of them are actually reprexes.
The main issue seems to be that you don't even load the package. As a result, take a look at your last line of pasted data, it says: Error in reprex(): could not find function "reprex".
What you're providing here (and elsewhere) is the copy of your console output. We can look at it to make judgments, but it doesn't help us answer your question any better, as we can't take your code, add a couple of lines and give it back to you thus solving your problem: we only can talk about your problem in abstract terms.
just to build on the last comment, the main limitation to helping you is that you are not providing sample data in a suitable format, you have already used datapasta on a previous topic, try to use it again.
I 2nd (or 3rd) the reprex request. It'll make the life much easier for people who want to help you.
Re:
the line ...mutate_at(vars(ends_with('2')), ~ .x * 10)... will only apply a function to any variable in your table that ends with '2'. Since A1, B1, C1 that seems to be working as expected.
Similar logic for vars(ends_with('2')
Note also that you can pipe together multiple mutate_iffunction calls together to have a single code block that sort your various issues.
Now, I would like to recode all values of -10 to be NAs. I know I could do it one buy one using a code like this:
df$A1[df$A1==-10] <- NA
df$A2[df$A2==-10] <- NA
df$B1[df$B1==-10] <- NA
...
df$D7[df$D7==-10] <- NA
but I hope that I could use mutate-if function again somehow because recoding applies to all the variables above.
Finally, I would like to do a clever recoding into new variables.
Is it possible to recode all numeric variables above into new variables with following logic: 10-60=0, 70-80=50 and 90-100=100? I need new set of recoded variables added to my df with _3 indicator like B2_3, C2_3, D3_3 as they represent 3 levels of responses (1=0, 2=50 and 3=100).
Ooops. I can see that mutate function does not change the data permanently. Would transform function better? I don't know how to convert mutate into transform but I need to have these changes saved in the df...
Well, my understanding of mutate is that this function changes values only temporarly because when I use summary() or head() or anything like that after using this function, I still can see initial, unchanged numbers.
I need to recode values of these variables permanently so when I run summary() before the recoding I can see Min:1, Max: 10. After the operation I should see Min:10, Max:100 for all recoded variables...
In general all dplyr functions don't modify dataframes in place, they create a new modified dataframe instead, if you want your changes to persist, then you need to explicitly assign them to the original dataframe (or a new one) with the assign operator <-
library(dplyr)
# This is made up data, you have to use your real data instead
df <- tribble(
~colA, ~A1, ~B2,
"a", 1, 3,
"b", 2, 5,
"c", 3, 6
)
df <- df %>%
mutate_at(vars(matches("[[:alpha:]]+[[:digit:]]+")), ~ .x * 10)
df
#> # A tibble: 3 x 3
#> colA A1 B2
#> <chr> <dbl> <dbl>
#> 1 a 10 30
#> 2 b 20 50
#> 3 c 30 60