printing entire long strings row by row (and removing a specific cell) in a dataframe r

Viewed 22

I have a dataframe d with 365 rows, which contains comments in the comments column, some of them are long (or NA). I want to view all of the comments, but I can only view only the first part of the comment using print() or I can view the entire comment using toString(), but then they are not separated by line, i.e. I get one huge string.

## look at all the comments, to see if there´s anything noteworthy 
## look at the comments separately, but can´t see the entire comment
d %>%
  filter(!is.na(comments)) %>%
  select(comments) %>% 
  print(n = nrow(d %>% filter(!is.na(comments))))

## view the entire comments, but in one bulk and not separated by case
d %>%
  filter(!is.na(comments)) %>%
  select(comments) %>% 
  toString()

## view one comment containing personal details
d %>%
  filter(grepl("Meine Daten sind", comments)) %>% 
  select(comments) %>%
  toString() 

## delete one comment with personal details
d1 <- d %>%
  mutate(across(.cols = comments,
                .fns = ~if_else(str_detect(.x, "Meine Daten sind"), "", .x)))

I tried googling and playing around with the arguments or mutating the entire column using toString(), but I couldn´t figure it out. Any ideas for how I can view all of the comments, the entire comments (and not just the first part), and separated by case?

(Another, but less important point is that I want to delete one comment which contains personal data. The solution above works, but to be completely honest, I copied it from the internet and am not sure why does it not work when I try using d %>% mutate(comments = if_else(str_detect(comments, "Meine Daten sind"), "", comments)) -> does it have to be as bulky as the (working!) solution above, or is there a more elegant way to do it?)

Thanks a lot in advance!

Guy

1 Answers

Try:

d$comments
unique(d$comments)
na.omit(unique(d$comments))

And to remove a comment with a particular string, you could use some variant of:

d[grepl("Meine Daten sind", d$comments), "comments"] <- NA
Related