Converting first column to rownames
2
0
Entering edit mode
John ▴ 30
@john-9676
Last seen 2.9 years ago

Hi,

T2LDLFC1 <- T2LDLFC %>% add_rownames()
T2SPLFC1 <- T2SPLFC %>% add_rownames()
T2LFC <- full_join(T2LDLFC1, T2SPLFC1)

> head(T2LFC)
Source: local data frame [2 x 3]

         rowname               T2_LD        T2_SP
         (chr)                (dbl)         (dbl)
1 10002:106603563             0.13508203   0.1351472
2 10003:106603561             -0.05005979  -0.3629763

I want to make the first column (rowname) to be my rowname without a column name instead of 1,2,....

I tried rownames(T2LFC) <- T2LFC[,1] without success.

Thank you for the help.

J.

rstudio • 18k views
ADD COMMENT
0
Entering edit mode
@steve-lianoglou-2771
Last seen 8 hours ago
Denali

This is more of a magrittr/dplyr question, so not really the appropriate forum, however I'll make a few notes.

For follow up questions, or future questions like these, you should go over to the manipulatr google group, which seems to be the forum to go to for all questions realted to plyr, reshap2, dplyr, and tidyr (or stackoverflow would also work, too, I think)

Part of the dplyr/tidy manifesto is that "rownames are bad", and a few of the dplyr verbs (like mutate) actually strip rownames from your data.frame, witness:

R> library(magrittr)
R> library(dplyr)
R> head(mtcars)
                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4        | 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag    | 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710       | 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive   | 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout| 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant          | 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

R> mutate(mtcars, score=rnorm(nrow(mtcars))) %>% head

    mpg cyl disp  hp drat    wt  qsec vs am gear carb      score
1| 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 -0.6913923
2| 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 -0.6626077
3| 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 -0.6879882
4| 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 -0.9331001
5| 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 -0.5598960
6| 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 -0.1118152

So, first thing to note is that it looks like you're working with a dplyr::tbl_df and not a normal data.frame, and no matter how hard you try, you may not be able to add rownames to it all (or you might not see them when printing tbl_df to the console), so you might have to convert your back back to a normal data.frame to get the behavior you're probably expecting.

Second, I think you're calling the wrong function. add_rownames() takes the rownames from the current data.frame and adds them as a new column to it (presumably because you will soon be stripping (or losing) the rownames in your data manipulation piping chain). The function you want to use to add rownames is magrittr::set_rownames

ADD COMMENT
0
Entering edit mode
John ▴ 30
@john-9676
Last seen 2.9 years ago

Thank you Steve.

 

I actually wanted to make row names from existing column(first one) and remove the first column. Converting the tbl_df back to data.frame helps.

T2SPLFC1 <- data.frame()
T2LDLFC1 <- data.frame()
T2LDLFC1 <- T2LDLFC %>% add_rownames()
T2SPLFC1 <- T2SPLFC %>% add_rownames()
T2LFC <- full_join(T2LDLFC1, T2SPLFC1)

T2LFCx <- data.frame(T2LFC)
rownames(T2LFCx) <- T2LFCx[,1]
T2LFCx <- T2LFCx[,-1]
T2LFCx <- na.omit(T2LFCx)

> head(T2LFCx)
                    T2_LD     T2_SP
10130:106603456 0.5774397 1.4970185
10188:106603639 1.0147191 1.5555289
10424:106603870 0.6308402 1.0958185
10473:100196411 0.6483884 0.7480247
10745:106604170 1.2062766 1.8775748
1080:106604807  0.5418450 0.6718973
>

Do you think this is OK? I am just newbie to R :P

Thanks for the help.

J.

ADD COMMENT
1
Entering edit mode

I think we're on different wavelengths here, as it's not quite clear what you're after. If the resulting data.frame has the results you expected, then I guess this is OK, but only you can make that call.

ADD REPLY

Login before adding your answer.

Traffic: 317 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6