replacing values of a dataframe column using values of a list and list name in R

我是研究僧i 提交于 2021-02-11 06:59:50

问题


I would like to replace Values inside of a column with a list name conditional upon the values being inside the list values:

df <- data.frame(Activity = c("Checking emails", "Playing games", "Reading", "Watching TV", 
                         "Watching YouTube", "Watching TV", "Relaxing", "Getting ready", 
                         "Working/ studying", "Relaxing"))

mylist <-list(Tech_activity = c("Browsing social media", "Checking emails", 
"Video calling", "On my computer/ PC", "Watching YouTube", "Browsing the internet", 
"On my phone", "Watching TV"), Socialising = c("Spending time with friends", 
"Chatting/ talking/ having a conversation", "Spending time with family"
), Work = "Working/ studying", Transport = c("Travelling", "Walking", 
"Driving"), Household = c("Housework", "Cooking"), Leisure = c("Exercising/ Working out", 
"Getting ready", "Exercising/ working out", "Hobbies eg knitting", 
"Playing games", "Shopping", "Eating", "Listening to music", 
"Reading", "Smoking", "Playing with pets", "Personal caring", 
"Personal care", "Nothing", "Relaxing", "Waiting")) 

So if the data frame value is in the values of an element in the list, then replace the df with that element name, if not skip that element and check the next element of the list and so on. (Please excuse the double for-loop).

for (i in df$Activity){
  for (j in mylist){
    if (i %in% mylist[j]){
      i <- names(mylist[j])
    }
  }
}

Thank you for any help in advance.


回答1:


You can make mylist as dataframe and then merge it with df.

merge(df, stack(mylist), by.x = 'Activity', by.y = 'values')

A tidyverse way would be :

library(tidyverse)

enframe(mylist) %>%
  unnest(value) %>%
  right_join(df, by = c('value' = 'Activity'))

#   name          value            
#   <chr>         <chr>            
# 1 Tech_activity Checking emails  
# 2 Tech_activity Watching YouTube 
# 3 Tech_activity Watching TV      
# 4 Tech_activity Watching TV      
# 5 Work          Working/ studying
# 6 Leisure       Getting ready    
# 7 Leisure       Playing games    
# 8 Leisure       Reading          
# 9 Leisure       Relaxing         
#10 Leisure       Relaxing         



回答2:


In base R:

matches <- unlist(lapply(mylist, function(x) which(df$Activity %in% x)))
df$Activity[matches] <- gsub("\\d+$", "", names(matches))

df
#>         Activity
#> 1  Tech_activity
#> 2        Leisure
#> 3        Leisure
#> 4  Tech_activity
#> 5  Tech_activity
#> 6  Tech_activity
#> 7        Leisure
#> 8        Leisure
#> 9           Work
#> 10       Leisure



回答3:


We can use tidyverse

library(tibble)
library(purrr)
library(dplyr)
enframe(mylist, value = 'Activity') %>%
      unnest(c(Activity)) %>%
      inner_join(df)

-output

# A tibble: 10 x 2
#   name          Activity         
#   <chr>         <chr>            
# 1 Tech_activity Checking emails  
# 2 Tech_activity Watching YouTube 
# 3 Tech_activity Watching TV      
# 4 Tech_activity Watching TV      
# 5 Work          Working/ studying
# 6 Leisure       Getting ready    
# 7 Leisure       Playing games    
# 8 Leisure       Reading          
# 9 Leisure       Relaxing         
#10 Leisure       Relaxing         


来源:https://stackoverflow.com/questions/65234685/replacing-values-of-a-dataframe-column-using-values-of-a-list-and-list-name-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!