0

I have the data.frame below.

> Chr   Chr
> A     E
> A     F
> A     E
> B     G
> B     G
> C     H
> C     I
> D     E

and... I want to convert the dataset as belows as you may be noticed. I want to coerce all chr vectors into an row.

chr chr
A    E,F
B    G
C    H,I
D    E

they are all characters, so I tried to do several things so that I want to make.

Firstly, I used unique function for FILTER <- unique(chr[,15])1st column and try to subset them using FILTER data that I created using rbind or bind rows function.

Secondly, I tested to check whether my idea works or not

FILTER <- unique(Top[,15])

NN <- data.frame()

for(i in 1 :nrow(FILTER)){
  result = unique(Top10Data[TGT == FILTER[i]]$`NM`)) 
  print(result)
} 

to this stage, it seems to be working well.

The problem for me is that when I used both functions, the data frame only creates 1 column and ignored the others vector (2nd variables from above data.frame) all.

Only For the chr [1,1], those functions do work well, but I have chr vectors such as chr[1,n], which is unable to be coerced.

here's my code for your reference.

FILTER <- unique(Top[,15])

NN <- data.frame()

for(i in 1 :nrow(FILTER)){
  CGONM <- rbind(NN,unique(Top10Data[TGT == FILTER[i]]$`NM`)) 
}

1 Answers1

0

Base R solutions:

# Solution 1: 

df_str_agg1 <- aggregate(var2~var1, df, FUN = function(x){
  paste0(unique(x), collapse = ",")})

# Solution 2: 

df_str_agg2 <- data.frame(do.call("rbind",lapply(split(df, df$var1), function(x){
    data.frame(var1 = unique(x$var1), 
               var2 = paste0(unique(x$var2), collapse = ","))
      }
    )
  ),
  row.names = NULL
)

Tidyverse solution:

library(tidyverse)
df_str_agg3 <- 
  df %>% 
  group_by(var1) %>% 
  summarise(var2 = str_c(unique(var2), collapse = ",")) %>% 
  ungroup()

Data:

df <- data.frame(var1 = c("A", "A", "A", "B", "B", "C", "C", "D"),
                 var2 = c("E", "F", "E", "G", "G", "H", "I", "E"), stringsAsFactors  = FALSE)
hello_friend
  • 5,682
  • 1
  • 11
  • 15