0

I have an excel file with 2 columns (1st column with headers & 2nd with data) and I'm trying it to transpose it(1st row with headers & 2nd with data). For some reason my code is only working for the data with >= 2rows.

  Input Excel:

enter image description here

    Expected Output:

enter image description here

    Code:
    df <- df_customer_upload()                     # original excel file
    df <- t(df_customer_upload())                  # Transposed excel file, in matrix format
    colnames(df) <- (df[1, ])                      # Copies 1st row to the header
    df <- df[-1,]                            # after copying 1st row as header, deletes the 1st row
    View(df)

Output I got: enter image description here

Any help would be appreciated. Thanks in advance!!

Mounica
  • 17
  • 5
  • Try these solutions : https://stackoverflow.com/questions/11322801/transpose-reshape-dataframe-without-timevar-from-long-to-wide-format and https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Ronak Shah Sep 11 '20 at 08:42
  • 1
    Try `df <- df[-1, , drop = FALSE]`. In the case of two rows the matrix is by default simplified to a vector when dropping one row (or column). To prevent this add argument drop=FALSE. – stefan Sep 11 '20 at 09:16
  • Hi @stefan, If I place drop =FALSE, it is creating an empty row with NA's which I don't want. Any idea about it? – Mounica Sep 11 '20 at 11:34
  • @RoshmithaReddy Unfortunately no. But I don't think that this is related to drop=FALSE as it only prevents the conversion to a vector. – stefan Sep 11 '20 at 11:57

0 Answers0