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:
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:
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.