sherek_66
sherek_66

Reputation: 531

how to change a column with some information in the data set?

I have columns household , persons in each household, tour (each tour contains different trips for each person) , trip (number of trips in each tour) , and mode ( mode of travel of each person in each trip)

I want change mode column with respect of tour column as the following

mood== car if there exist at least one trip in the tour with mode car

mood==non-car if non of trips in a tour has mode=car

example:

   household.  person.  trip.   tour.    mode
       1         1        1       1       car
       1         1        2       1       walk
       1         1        4       1       bus
       1         1        1       2       bus
       1         1        2       2       walk
       1         2        1       1       walk
       1         2        2       1       bus
       1         2        3       1       walk
       2         1        1       1       walk
       2         1        1       1       car

output

   household.  person.  trip.   tour.    mode
       1         1        1       1       car
       1         1        2       1       car
       1         1        4       1       car
       1         1        1       2       non-car
       1         1        2       2       non-car
       1         2        1       1       non-car
       1         2        2       1       non-car
       1         2        3       1       non-car
       2         1        1       1       car
       2         1        1       1       car

Upvotes: 2

Views: 85

Answers (1)

akrun
akrun

Reputation: 887148

We can group by 'household.', 'person.', 'tour.' and change the 'mode' to two values by checking if there are any 'car' in the column. In that case, convert it to a numeric index by adding 1 (TRUE -> 2, FALSE ->1) and based on this index, we pass a vector of strings to replace the index

library(dplyr)
df1 %>% 
    group_by(household., person., tour.) %>%
    mutate(mode = c('non-car', 'car')[1+any(mode == "car")])
# A tibble: 10 x 5
# Groups:   household., person., tour. [4]
#   household. person. trip. tour. mode   
#        <int>   <int> <int> <int> <chr>  
# 1          1       1     1     1 car    
# 2          1       1     2     1 car    
# 3          1       1     4     1 car    
# 4          1       1     1     2 non-car
# 5          1       1     2     2 non-car
# 6          1       2     1     1 non-car
# 7          1       2     2     1 non-car
# 8          1       2     3     1 non-car
# 9          2       1     1     1 car    
#10          2       1     1     1 car    

data

df1 <- structure(list(household. = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L), person. = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), 
    trip. = c(1L, 2L, 4L, 1L, 2L, 1L, 2L, 3L, 1L, 1L), tour. = c(1L, 
    1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), mode = c("car", "walk", 
    "bus", "bus", "walk", "walk", "bus", "walk", "walk", "car"
    )), class = "data.frame", row.names = c(NA, -10L))

Upvotes: 4

Related Questions