rowname

rownames and colnames with specific value

眉间皱痕 提交于 2019-12-02 02:20:48
I have this matrix and i want to get a 2 column matrix, where one column has the rowname and the other column, the colname of cells with value 1 x X1 X2 X3 X1 1 0 1 X2 0 1 0 X3 0 1 1 X4 1 0 0 str(x) num [1:886, 1:886] 1 0 1 1 1 0 1 1 1 1 I want a matrix like this # X1 X1 # X1 X3 # X2 X2 # X3 X2 # X3 X3 # X4 X1 which are the pairs that have value=1 Thanks in advance, A. here another option : mm <- expand.grid(rownames(mat),colnames(mat))[as.vector(mat==1),] Var1 Var2 1 X1 X1 4 X4 X1 6 X2 X2 7 X3 X2 9 X1 X3 11 X3 X3 And to get the OP display , we order by the first column: mm[order(mm$Var1),]

How to remove the rownames from the file

牧云@^-^@ 提交于 2019-12-01 12:42:34
I have a file which looks like this.. "Locations" "X9442" "X5997" "1" "cg00000957" 0.87 0.86 "2" "cg00001349" 0.78 0.78 "3" "cg00001583" 0.06 0.08 "4" "cg00002028" 0.01 0.01 I wish to remove the row names and make it look like as follows: "Locations" "X9442" "X5997" "cg00000957" 0.87 0.86 "cg00001349" 0.78 0.78 "cg00001583" 0.06 0.08 "cg00002028" 0.01 0.01 I tried various things, but I am not able to remove those indexes while doing write.table. Also sometimes the header "Locations" shifts its column. Can someone help me out how do I achieve the above in R or linux? Look at the help file:

Why do identical dataframes become different when changing rownames to the same

不问归期 提交于 2019-12-01 07:54:45
I've come across a strange behavior when playing with some dataframes: when I create two identical dataframes a,b , then swap their rownames around, they don't come out as identical: rm(list=ls()) a <- data.frame(a=c(1,2,3),b=c(2,3,4)) b <- a identical(a,b) #TRUE identical(rownames(a),rownames(b)) #TRUE rownames(b) <- rownames(a) identical(a,b) #FALSE Can anyone reproduce/explain why? This is admittedly a bit confusing. Starting with ?data.frame we see that: If row.names was supplied as NULL or no suitable component was found the row names are the integer sequence starting at one (and such row

Specifying row names when reading in a file

强颜欢笑 提交于 2019-11-30 06:05:19
I have a .txt file that contains row names. However, R set the row names as the first column. If you used read.table() (or one of it's ilk, e.g. read.csv() ) then the easy fix is to change the call to: read.table(file = "foo.txt", row.names = 1, ....) where .... is other arguments you needed/used. The row.names argument take the column number of the data file from which to take the row names. It need not be the first column. See ?read.table for details/info. If you already have the data in R and can;t be bothered to re-read it, or is came from another route, just set the rownames attribute and

Row names & column names in R

不羁的心 提交于 2019-11-29 23:11:34
Do the following function pairs generate exactly the same results? Pair 1) names() & colnames() Pair 2) rownames() & row.names() Dirk Eddelbuettel As Oscar Wilde said Consistency is the last refuge of the unimaginative. R is more of an evolved rather than designed language, so these things happen. names() and colnames() work on a data.frame but names() does not work on a matrix: R> DF <- data.frame(foo=1:3, bar=LETTERS[1:3]) R> names(DF) [1] "foo" "bar" R> colnames(DF) [1] "foo" "bar" R> M <- matrix(1:9, ncol=3, dimnames=list(1:3, c("alpha","beta","gamma"))) R> names(M) NULL R> colnames(M) [1]

Specifying row names when reading in a file

末鹿安然 提交于 2019-11-29 02:07:07
问题 I have a .txt file that contains row names. However, R set the row names as the first column. 回答1: If you used read.table() (or one of it's ilk, e.g. read.csv() ) then the easy fix is to change the call to: read.table(file = "foo.txt", row.names = 1, ....) where .... is other arguments you needed/used. The row.names argument take the column number of the data file from which to take the row names. It need not be the first column. See ?read.table for details/info. If you already have the data

Row names & column names in R

a 夏天 提交于 2019-11-28 19:36:32
问题 Do the following function pairs generate exactly the same results? Pair 1) names() & colnames() Pair 2) rownames() & row.names() 回答1: As Oscar Wilde said Consistency is the last refuge of the unimaginative. R is more of an evolved rather than designed language, so these things happen. names() and colnames() work on a data.frame but names() does not work on a matrix: R> DF <- data.frame(foo=1:3, bar=LETTERS[1:3]) R> names(DF) [1] "foo" "bar" R> colnames(DF) [1] "foo" "bar" R> M <- matrix(1:9,

How to remove rows of a matrix by row name, rather than numerical index?

六眼飞鱼酱① 提交于 2019-11-28 06:15:47
I have matrix g : > g[1:5,1:5] rs7510853 rs10154488 rs12159982 rs2844887 rs2844888 NA06985 "CC" "CC" "CC" "CC" "CC" NA06991 "CC" "CC" "CC" "CC" "CC" NA06993 "CC" "CC" "CC" "CC" "CC" NA06994 "CC" "CC" "CC" "CC" "CC" NA07000 "CC" "CC" "CC" "CC" "CC" > rownames(g)[1:2]->remove > remove [1] "NA06985" "NA06991" > g[-remove,] Error in -remove : invalid argument to unary operator Is there a simple way to do what I want to do here (remove the ID's referenced in the vector 'remove' from matrix g ? Note: this is just a model for what I actually want to do, please don't say just do g[-(1:2), ] , I need

Merging more than 2 dataframes in R by rownames

依然范特西╮ 提交于 2019-11-27 18:31:22
I gather data from 4 df's and would like to merge them by rownames. I am looking for an efficient way to do this. This is a simplified version of the data I have. df1 <- data.frame(N= sample(seq(9, 27, 0.5), 40, replace= T), P= sample(seq(0.3, 4, 0.1), 40, replace= T), C= sample(seq(400, 500, 1), 40, replace= T)) df2 <- data.frame(origin= sample(c("A", "B", "C", "D", "E"), 40, replace= T), foo1= sample(c(T, F), 40, replace= T), X= sample(seq(145600, 148300, 100), 40, replace= T), Y= sample(seq(349800, 398600, 100), 40, replace= T)) df3 <- matrix(sample(seq(0, 1, 0.01), 40), 40, 100) df4 <-

What is about the first column in R's dataset mtcars?

瘦欲@ 提交于 2019-11-27 15:11:21
I think I am missing a fundamental concept about R's data frames. 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 The names of the cars here. Is this a column? I don't think so, because I am not able to access them via mtcars[,1] . And there is no column name/header for it.