Unify column the dataset and place this content below a table

Asked

Viewed 596 times

2

I have table I have a column that is a data set I need to separate this data and unify the "standard columns".

I’ve managed to separate the contents of the column by staying this way:

nrow  temp area   sep1 sep2  sep3   sep4  sep5  sep6    
 1      x     x    x     x     x     x    NA    NA  
 2      x     x    x     x     x     x    x     NA  
 3      x     x    x     x     NA    NA   NA    NA  
100000  x     x    x     x     x     x    x     x  

But I still need to do this:

nrow  temp area   sep1 
 1      x     x    x     
 2      x     x    x     
 3      x     x    x     
100000  x     x    x     
nrow  temp area   sep2 
 1      x     x    x     
 2      x     x    x     
 3      x     x    x     
100000  x     x    x     
nrow  temp area   sep3 
 1      x     x    x     
 2      x     x    x     
 3      x     x    NA
100000  x     x    x     
nrow  temp area   sep4 
 1      x     x    x     
 2      x     x    x     
 3      x     x    NA
100000  x     x    x    
nrow  temp area   sep5 
 1      x     x    NA
 2      x     x    x     
 3      x     x    NA
100000  x     x    x     
nrow  temp area   sep6 
 1      x     x    NA
 2      x     x    NA    
 3      x     x    NA    
100000  x     x    x    

For this I tried to separate the columns and join with a loop of repetition, but I cannot finish the code, below what I tried to do:

file_split = data.frame(colsplit(file_unic$UNIR, pattern=";",  names=paste0("sep", 1:34)))### separar coluna 

     for(i in 1:ncol(file_split)){
     uniao = cbind(file,sep) # juntar uma coluna previamente separada ao conjunto de dados
     }

However, I would still need to place one block below the other. How can I finish this?

1 answer

3


It’s not exactly what you want, but I think it might help you.

I will assume the following data.frame. Posting the expected result helps, but it would help more if you provided a more complete example in the question.

df <- data.frame(temp = c(10.1, 10.2, 10.3), 
                 area = c("A", "B", "C"), 
                 sep1 = c(1, 2, 3),
                 sep2 = c(10, 20, 30),
                 sep3 = c(100, 200, 300))

df;
  temp area sep1 sep2 sep3
1 10.1    A    1   10  100
2 10.2    B    2   20  200
3 10.3    C    3   30  300

Now the transformation that I think can help you.

library(tidyr);
gather(df, sep, value, -temp, -area)

  temp area  sep value
1 10.1    A sep1     1
2 10.2    B sep1     2
3 10.3    C sep1     3
4 10.1    A sep2    10
5 10.2    B sep2    20
6 10.3    C sep2    30
7 10.1    A sep3   100
8 10.2    B sep3   200
9 10.3    C sep3   300
  • i tried to do this for my data, but gave the following error Warning message: Attributes are not identical Across Measure variables; they will be Dropped @Marcosbanik

  • Not if allowed in this forum, but I can make my full code and file available in Dropbox?

  • 1

    would be better to change my example. For example, I’m suspicious that the contents of sep1, sep2, sep3 are just letters like 'a, b, c' or NA. if so, try using df[3:8] <- lapply(df[3:8], factor, levels=Letters). 3:8 is the range with the number of columns containing the seps. Check this reply from SOEN https://stackoverflow.com/a/25689246, I think it can help you.

  • 1

    I was able to solve the problem with your comment, I made a modification. I read the file with fread and used colClasses = "Character". Leaving everything as the same class your model worked. Thank you very much =)

Browser other questions tagged

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