I'd like to transpose this data frame:
So I use this line of code
perCountry <- data.frame(t(perEnergy))
And I get this result:
But I'd like it without the first row (X1,X2,X3...) and with the country name at this spot. How could I do ?
I'd like to transpose this data frame:
So I use this line of code
perCountry <- data.frame(t(perEnergy))
And I get this result:
But I'd like it without the first row (X1,X2,X3...) and with the country name at this spot. How could I do ?
The problem is that you would have two different columns for each country: one for % and one for MW. It might be easier to work with if you have two different data frames:
percentages <- data.frame(t(perEnergy[perEnergy$a == "%",]))
MW <- data.frame(t(perEnergy[perEnergy$a == "[MW]",]))
You can turn the first row into column names like this:
names(percentages) <- percentages[1,]
names(MW) <- MW[1,]
Then remove the unwanted first row like this:
percentages <- percentages[-1,]
MW <- MW[-1,]
Finally, if you want to stick them back together again, you will need unique row names. You might do something like this:
rownames(percentages) <- paste0(rownames(percentages), "_percent")
rownames(MW) <- paste0(rownames(MW), "_MW")
So that finally you can join the two dataframes together:
perCountry <- rbind(percentages, MW)
Unfortunately I cannot test the above code, since you did not post any usable data in your question.