[R] data manipulation

Berend Hasselman bhh at xs4all.nl
Wed May 15 08:53:12 CEST 2013


On 15-05-2013, at 08:15, catalin roibu <catalinroibu at gmail.com> wrote:

> Hello all!
> I have a problem with my data.
> My initial data is a list years and months (see below). I want to transpose
> my data (12 rows (months) and year values as column). I try t(spi3), but
> the year values do not appear as column names, but as values.

As expected since the column year is a column of numbers and is printed with the appropriate number of decimals for each column.
You can get what you want in two ways.

I've called your initital dataframe DF.

First transpose DF, then set the column names to the contents of the year row and finally delete the first row (the year row).

DF.t <- t(DF)
colnames(DF.t) <- DF.t["year",]
DF.t <- DF.t[-1,]
DF.t

The second method is to put DF in the form you apparently want and then transpose.
So set the rownames of DF to the column "year", delete the year column and then transpose.

rownames(DF) <- DF[,"year"]
DF <- DF[,-1]
t(DF)

Berend



More information about the R-help mailing list