I'm looking into the sourcecode of a function in R and part of it is this:
dat <- data.frame(obsnames = row.names(PC$x), PC$x)
As far as I know you can assign one column as row.names, so the second PC$x doesn't make sense to me. Any idea?
I get the sourcecode using this:
library(cummeRbund)
showMethods(PCAplot)
getMethod("PCAplot","CuffData")
The commands above returns THIS text.
Any help it's appreciated.
Whole idea of the statement is to convert rownames
into one of the columns of a dataframe
and append it to the actual data.
Point to Note: rownames(any_object)
is not a column in the data frame.
I. Creating data frame df
with one column 'Data`
df <- data.frame(Data = c("Stack","OverFLow","Stack","EXchange"))
df
# Data
# 1 Stack
# 2 OverFLow
# 3 Stack
# 4 EXchange
II. Converting rownames
of df
into a column and binding it to Data
column of df
and storing it to new data frame df1
To convert rownames
into one of the columns of a data frame, it is just creating a new data frame df1
where your first column
is row names
and second column
is the actual first column of your old data.
df1 <- data.frame(obsnames = rownames(df),df$Data)
df1
# obsnames df.Data
# 1 1 Stack
# 2 2 OverFLow
# 3 3 Stack
# 4 4 EXchange
III. Changing rownames
of a data frame
If you want to change rownames
of any data frame, here it would go like this
rownames(df1) <- c("first","second","third","fourth")
df1
# obsnames df.Data
# first 1 Stack
# second 2 OverFLow
# third 3 Stack
# fourth 4 EXchange
IV. To bind rownames
to whole dataframe
df <- data.frame(AA = 31:33, BB = 21:23, CC = 11:13, DD = 1:3)
df
# AA BB CC DD
# 1 31 21 11 1
# 2 32 22 12 2
# 3 33 23 13 3
df1 <- data.frame(obsnames = rownames(df),df)
df1
# obsnames AA BB CC DD
# 1 1 31 21 11 1
# 2 2 32 22 12 2
# 3 3 33 23 13 3