-1

I have the following data set

Data that needs combining

I'd like it such that cell [2,6] retains its current content with the addition of the cell below it, separated by a "," I have found paste() functions for concatenating columns into new columns but can't find an answer for specific cell combinations. Any help appreciated.

Example

For example, I'd like the highlighted cell in the above to read John, Mary

s__
  • 9,270
  • 3
  • 27
  • 45
hugada
  • 1
  • 4
  • Please provide reproducible example using dput with input and required output as the question is vague. – Nareman Darwish Jul 22 '21 at 11:01
  • I've edited the post for a bit more clarity – hugada Jul 22 '21 at 11:11
  • Here's a way of how you can make a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nareman Darwish Jul 22 '21 at 11:14
  • `library(dplyr); data %>% group_by(Type) %>% summarise(name = paste0(Name, collapse = ', '))` ? – s__ Jul 22 '21 at 12:22
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Images are not the right way to share data/code. – Ronak Shah Jul 22 '21 at 13:30

1 Answers1

0

Here is a way on how you can paste values with the values in below row.

# Loading required libraries
library(dplyr)

# Creating sample data
example <- data.frame(Type = c("Director", NA_character_),
                      Name = c("John", "Mary"))

example %>%
  # Get value of the below row
  mutate(new_col = lead(Name, 1),
         # If Type is director then paste names else null
         new_col = ifelse(Type == "Director", paste(Name, new_col, sep = ", "), NA_character_))
Nareman Darwish
  • 1,251
  • 7
  • 14
  • In the end I used the following: ```df_AEC <- AEC_data_global_front %>% add_row(Type = "Directors", Details = "CONCAT")``` ```df_AEC[8,2] = paste(df_AEC[6,2], df_AEC[7,2], sep = ", ") AEC_contacts <- df_AEC[-c(6, 7), ]``` Creating a new column then creating a concatenated cell and deleting the previous column. – hugada Jul 26 '21 at 13:12