I am using R to transpose a data frame from an excel file (mydata.xls). The original data frame looks like this:
ID AB_1 AB_2 AB_3 AB_4 AB_5
Variable1 1 2 3 4 5
Variable2 1.5 2.8 4.5 5.6 7.8
This is what I would like to achieve:
ID Variable1 Variable2
AB_1 1 1.5
AB_2 2 2.8
AB_3 3 4.5
AB_4 4 5.6
AB_5 5 7.8
Here is what I did, according to the response on a similar post in the past.
Library(XLConnect)
x=loadWorkbook("mydata.xls")
y=readWorksheet(x,"Summary")
z=setNames(data.frame(t(y[,-1])),y[,1])
However, here is what I got:
z
Variable1 Variable2
AB_1 1 1.5
AB_2 2 2.8
AB_3 3 4.5
AB_4 4 5.6
AB_5 5 7.8
Two problems are noticed: 1."ID" is missing. 2. when I checked the first column of the new data frame, the second column was returned (see below)
z[,1]
[1] 1 2 3 4 5
I wonder 1) what happened to the "ID" and that entire column? 2) How the issues could be fixed?