Concatenate values within the same data frame columns in R software

Asked

Viewed 2,458 times

2

fDados de Concentração plasmática

Could someone help me? Anyway I’m grateful!!!

2 answers

2

Search the fa function help paste. If your data frame is df can do:

re1<- apply(df[1:3,-1],2,paste,collapse="_")
nomes<- c(df[3,1],re1)

Novo df:

df1 <- df[-c(1:3),]
colnames(df1)<- nomes
  • I applied his suggestion but he did not include the variable V1. That is, he only considered the variables V2, V3, V4, V5. In addition to that I also need these concatenated cells to remain within the data frame. When I asked to print the date frame the results were as before concaternation.

  • That’s another question. You want to delete the first 2 rows and replace the column names in a new data frame! As you have not posted a reproducible example see a suggestion in the updated reply.

  • You guessed Robert!!! In a second moment I would ask to exchange the result of the concaternation for the result of V1, V2, V3, V4 and V5. Now on the other hand, when I reproduced this your last code I checked that when I print df1 instead of the variable "Time" (in the header) appears the number "16". I need "Time" to appear. What’s going on?

  • What can be changed in this code that you provided above to appear the name "Time" in the header instead of the number "16"? Thank you so much for the excellent help!!!

-2

Ready I found out....

The right thing would be:

N_row_concatenate<-3 # Digite ao lado o número de   linhas que você deseja 
                     #concaternar para formar o nome da nova variável!!!

Head_Name <-apply(Df[1:N_row_concatenate,-(1+ncol(Df))],2,paste,collapse="_")
New_Df <- Df[-c(1:N_row_concatenate),]
colnames(New_Df)<- Head_Name
New_Df_long <- melt(New_Df, id=c(Head_Name[1:1])) 

Anyway thank you very much for the initial idea. It was of great value!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.