vendredi 26 novembre 2021

how to fill a data frame based on another table using loop

I have a data frame like below: data:

df<-tibble(id=c("ls1","ls1","ls2","ls4"),
           symbol=c("a","a","b","df"),
           var=c("-","gh","gh","lm"))

I want to convert to another data frame like below:

ls1 lsp10   ls02    ls6 
a   _   gh  _   _ 
a   _   _   _   _ 
b   _   _   gh  _ 
df  _   _   _   lm

To this end I am using the code below loop but it is not working?

for(i in 1:nrow(data)) {
  
  for(j in 1:nrow(data)) {
    
    if(identical(data[1,1], data[1,1]) && identical(data[1,2], data[1,2]) && data[1,3] = data[1,3]){
      
      data[i,3] <- paste0(data[i,3],";",data[j,3])
      data<- data[-j,]
      
    }}}

data file doesn't change! Any idea?

Aucun commentaire:

Enregistrer un commentaire