How do I manipulate my dataframe to keep the columns with matching rownames?

Noahp555

I have a data frame with 77 rows and 460 columns. The first column represents the rsID for each row titled "RS_number". The columns are each labeled by a SNP rsID (e.g., rs4751).

I need to limit this data frame to the dimensions 76 X 76, reflecting the same column names as the variable "RS_number". My first thought is to make the row.names equal to the first column "RS_number", but I am not sure how to move forward with this AND how to limit the columns to the same identifiers as the rownames.

Below is the code I used to create the data frame and a sample of the data frame:

'''newdf = concatenated[concatenated$RS_number %in% colnames(hours)[3:76],] %>% 
 as.data.frame()'''



     RS_number rs1 rs2 rs3 rs4 rs10
[,1]  rs1       1.0 0.2 0.3 0.4 NA
[,2]  rs2       0.0 1.0 0.0 NA  0.2
[,3]  rs3       0.2 0.1 1.0 NA  NA
[,4]  rs4       0.0 0.1 0.5 1.0 NA
[,5]  rs5       NA  0.1 NA  0.2 NA
[,6]  rs9       0.5 0.4 0.1 0.0 0.6

I would like my data frame to only keep those columns and RS_numbers that are common including rs1, rs2, rs3, rs4, and exclude rs5, and rs10.

akrun

We can use intersect with the 'Rs_number' column values and the column names of 'df1', then, concatenate the 'Rs_number' with the intersected elements to select the columns

nm1 <- intersect(df1$Rs_number, names(df1))
df1[c("Rs_number", nm1)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I order the rownames of a dataframe in R?

python - how do i assign columns to my dataframe?

how do I change rownames in R shiny?

How can I keep all the rows in my dataframe that have the same values in all columns?

How do I manipulate a Dataframe with Pivot_Table in Python

How do I use ioctl() to manipulate my kernel module?

How do I compare two columns and print non matching values in each dataframe?

How do i keep the order of my SQL columns in Python when saving?

How do I convert multiple `string` columns in my dataframe to datetime columns?

How do I keep the screen on in my App?

How do i plot a pandas.DataFrame.plot.bar() with this weird columns in my pandas dataframe?

How do I keep the timezone of my index when serializing/deserializing a Pandas DataFrame using JSON

Fill a new list in R matching columns and rownames

How do I manipulate WebGrids?

How do I manipulate this string

How do I replace a string value with a NULL in PySpark for all my columns in the dataframe?

How do I conditionally remove columns from my dataframe without using for loops?

How do I find which columns in my pandas dataframe contain a list?

How do I pivot my dataframe multiple times in pandas while creating a new column merging multiple columns?

How do I sort my columns so that they match the order of rows in a different dataframe?

How do I combine the float values of two columns and put it in an another column of my dataframe?

How can I manipulate dataframe columns with different values from an external vector (with dplyr)

How do I combine columns of my dataframe to create one datetime column which I can use as my index?

How do I split my list into columns

How to keep two Lots when matching on unique combinations across multiple R dataframe columns

How do I create this Pandas column matching most columns?

How do i return multiple matching columns into 1 column with VLOOKUP

How do I highlight all columns matching a number with jquery

How do I sort the order of columns in a dataframe by another dataframe in R?