简体   繁体   中英

Finding and replacing strings in cells of a matrix in R

I'm trying to process a survey, in which one of the questions asks the respondents to name a friend. Now I have a matrix like this:

https://i.imgur.com/pW5ZcTI.png

I want to save these results in a relational database. I have assigned every person a unique ID, and want the answers to be saved as a last of ID's. So that the table looks like this:

在此处输入图片说明

My code so far:

i've tried

df$name %in% df$friends

which did not give any results. I'm now trying to use a for loop with str_detect:

friends <- df$friends
names <- df$name
for (i in 1:length(names)) {
  friends_called <- str_detect(friends, names[i])
  id_index <- grep(names[i], df$name)
  id <- df$id[id_index]
  for (j in 1:length(friends_called)) {
    if(friends_called[j] == T) {
      df$friends_id[j] <- paste(df$friends_id[j], id, ",", sep="")
    }
df$friends <- df$friends_id

But I have some issues with it:

  1. It's not working
  2. It uses two loops, which i'm used to from writing python but I read that i should avoid them in R
  3. The string matching needs to be fuzzy (If Anna wrote "Jon" instead of "John", it should still match.

Does anyone have suggestions on how to tackle this?

You can do this without a loop in tidyverse as follows:

df %>%
  mutate(friends = map(friends, ~ df %>% 
                         filter(str_detect(.x,name)) %>% 
                         select(id) %>% 
                         unlist() %>% 
                         paste(collapse = ',')))

gives

   id   name friends
1 a1d   John b2e,c3f
2 b2e   Anna     a1d
3 c3f Denise

or with base R you can use sapply:

df$friends <- sapply(friends, function(x) paste(id[str_detect(x,name)],collapse = ','))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM