1
votes

I have a data frame called cleandata and need to change values on column age.

I can find the values to be replaced with filter and select functions from dplyr.

> str(cleantrain)
'data.frame':   891 obs. of  9 variables:
 $ train$PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ survived         : Factor w/ 2 levels "0","1": 1 2 2 2 1 1 1 1 2 2 ...
 $ Title            : Factor w/ 17 levels "Capt","Col","Don",..: 12 13 9 13 12 12 12 8 13 13 ...
 $ fare             : num  7.25 71.28 7.92 53.1 8.05 ...
 $ sbsp             : int  1 1 0 1 0 0 0 3 0 1 ...
 $ parch            : int  0 0 0 0 0 0 0 1 2 0 ...
 $ alone            : Factor w/ 2 levels "0","1": 1 1 2 1 2 2 2 1 1 1 ...
 $ familysize       : Factor w/ 9 levels "1","2","3","4",..: 2 2 1 2 1 1 1 5 3 2 ...
 $ age              : num  22 38 26 35 35 NA 54 2 27 14 ...

# Column title is equal to "Master" and Column age is NA
> cleantrain %>% filter(Title == "Master" & is.na(age))
  train$PassengerId survived  Title    fare sbsp parch alone familysize age
1                66        1 Master 15.2458    1     1     0          3  NA
2               160        0 Master 69.5500    8     2     0         11  NA
3               177        0 Master 25.4667    3     1     0          5  NA
4               710        1 Master 15.2458    1     1     0          3  NA

I just need to replaces these NAs with 8. Using mutate as below will not update original cleantrain data.frame

>cleantrain %>% filter(Title == "Master" & is.na(age)) %>% mutate(age = 8) #will put the right info on the right place.

  train$PassengerId survived  Title    fare sbsp parch alone familysize age
1                66        1 Master 15.2458    1     1     0          3   8
2               160        0 Master 69.5500    8     2     0         11   8
3               177        0 Master 25.4667    3     1     0          5   8
4               710        1 Master 15.2458    1     1     0          3   8

#but not actually. when checking dataframe values remains NAS

>cleantrain %>% filter(Title == "Master" & is.na(age))

  train$PassengerId survived  Title    fare sbsp parch alone familysize age
1                66        1 Master 15.2458    1     1     0          3  NA
2               160        0 Master 69.5500    8     2     0         11  NA
3               177        0 Master 25.4667    3     1     0          5  NA
4               710        1 Master 15.2458    1     1     0          3  NA

Can I use mutate to do this? Any Dplyr/quick function that does not requires for/if loops? #learningR

1
You are not updating the orignal 'cleantrain; also, instead of filter, try replace i.e. cleantrain <- cleantrain %>% mutate(age = replace(age, Title == 'Master' & is.na(age), 8))akrun

1 Answers

0
votes

The replace solution of @akrun will work if you want to update rows with a fixed value. Generally, you have to use ifelse function, I believe:

cleantrain <- cleantrain %>% 
  mutate(age = ifelse(Title == 'Master' & is.na(age),
                      8,
                      age))