1

I would like to reshaping some rows of my database. In particular I have some row that it replicate for the Id column. I would like to convert this row in column. I provide a code that it represent a example of my database. I'm trying with t() and reshape but it doesn't do that I would. Can anyone give me any suggestions?

test<-data.frame(Id=c(1,1,2,3),
    St=c(20,80,80,20),
    gap=seq(0.02,0.08,by=0.02),
    gip=c(0.23,0.60,0.86,2.09),
    gat=c(0.0107,0.989,0.337,0.663))
Rohit
  • 1,967
  • 1
  • 12
  • 15
matte85
  • 35
  • 3
  • Can you show us what you expect the output to look like? – Rohit May 31 '19 at 11:23
  • Yes! I expect a final database with one Id for each rows. A final dataframe with 9 different column, something like: Id, St,gap,gip,gat,St2 gap2,gip2,gat2. The values with 2 is referee to a second row of principal database. – matte85 May 31 '19 at 11:57
  • @matte85 You should edit your question and add the expected output in it, it'll be easier for people to help you. – Gainz May 31 '19 at 13:31

1 Answers1

0
setNames(data.frame(t(test))[2:nrow(data.frame(t(test))),], test$Id)

          1      1      2      3
St  20.0000 80.000 80.000 20.000
gap  0.0200  0.040  0.060  0.080
gip  0.2300  0.600  0.860  2.090
gat  0.0107  0.989  0.337  0.663

It helps to provide an expected output. It this what you expected?

DSGym
  • 2,807
  • 1
  • 6
  • 18
  • Sure! I would like to ask other question. If I would like to maintain the Id ad row and so transponse the other variable? how can modify the code? Is there a possibility to have integer number for column st? – matte85 May 31 '19 at 14:47
  • To do this you should reordner the columns. There are multiple ways to do this: `test <- test %>% dplyr::select(St, Id, gap, gip, gat)`. You can not have integer numbers here: The reason is because variable types refer ALWAYS to the column. A column can only be a integer, numeric or string, but never a mix. Values get always coerced otherwise. If one value is a string, the whole column will be a string and so on. – DSGym May 31 '19 at 14:55
  • thanks for the availability. One last question (seriously) that came to mind now looking at the result of the operation. If I wanted to just transpose the line with the double id and bring it to the column,the other columns remain as they are how should I do it? – matte85 May 31 '19 at 15:42
  • Open a new question for this topic please. We are happy to help, but one post should not include multiple questions and answers :) – DSGym May 31 '19 at 17:23