How can I associate names from first column with repeating rows?

Mark

I have say this dataframe:

gene0   1   2   3
gene1   0   0   5
gene2   6   8   0
gene3   5   5   5
        0   0   5
        1   2   3

and I would like to associate numbers from "unnamed" columns with genes to have the following:

gene0   1   2   3
gene1   0   0   5
gene2   6   8   0
gene3   5   5   5
gene1   0   0   5
gene0   1   2   3

What is the best way to do it? Do I need to use linux or R for that?

akrun

We can use match from base R

a1 <- do.call(paste, df1[-1])    
df1$V1 <-  df1$V1[match(a1, unique(a1))]
df1$V1
#[1] "gene0" "gene1" "gene2" "gene3" "gene1" "gene0" 

Update

Using the OP's dataset

df1 <- read.csv("newest.csv", stringsAsFactors = FALSE)
df1$id[df1$id == ""] <- NA
a1 <- do.call(paste, df1[-1])    
df1$id <-  df1$id[match(a1, unique(a1))]
length(unique(df1$id))
#[1] 621    

head(df1$id, 20)
#[1] "pop13_110" "pop1_2"    "pop16_108" "pop2_10"   "pop2_2"    "pop2_3"    "pop2_4"    "pop2_5"    "pop2_6"    "pop2_7"    "pop2_8"   
#[12] "pop2_9"    "pop2_10"   "pop2_11"   "pop7_81"   "pop2_13"   "pop2_15"   "pop2_15"   "pop2_16"   "pop22_20" 
tail(df1$id, 20)
# [1] "pop22_2"   "pop22_3"   "pop22_4"   "pop22_5"   "pop22_8"   "pop22_9"   "pop13_60"  "pop16_131" "pop23_11"  "pop22_25"  "pop22"    
#[12] "pop22_14"  "pop22_15"  "pop22_32"  "pop22_28"  "pop16_56"  "pop22_18"  "pop9_9"    "pop22_21"  "pop22_22" 

data

df1 <- structure(list(V1 = c("gene0", "gene1", "gene2", "gene3", NA, 
NA), V2 = c(1L, 0L, 6L, 5L, 0L, 1L), V3 = c(2L, 0L, 8L, 5L, 0L, 
2L), V4 = c(3L, 5L, 0L, 5L, 5L, 3L)), class = "data.frame", 
row.names = c(NA, 
-6L))

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 concatenate rows into column names?

How can i order my rows from (first) a column and then (secondly) another column which is a specific starting time?

How do I pivot values from rows into column names in Postgres?

How can I make a new data set from names of rows?

How can I use Python to use repeating column value to separate rows in a dataframe?

How do I avoid repeating column names in a csv file in r?

How can I associate the First Person Controller in Unity with the VROneSDK and VROneSDKHead?

How can I get the selected column rows on top first and then the remaining rows that do not belong to the selected column in MySQL?

How to prevent Pandas Dataframe from repeating column names at every row?

How can I get column names from a table in SQL Server?

How can I get column names from a table in Oracle?

How can I extract the column names from ColumnDataSource in Bokeh 2.4.3?

Create a new table with first rows as the column names from old Table

Reshape based on repeating rows in first column in R

Pandas how can I extract by regex from column into multiple rows?

Can I get column names from a string?

How can I remove the rows with the same first column and lower second column in awk?

How can I add values from the first column to a new array?

How can i update the rows of a Dataframe with to make them a dictionary with the column names?

How can I output the names of rows which are NA in a given column in R?

How can I associate these models

SQL Server select all rows into single column with repeating column names

How can I change the column names of a list to the first row of each data frame in a loop?

how could i delete rows with repeating/duplicate index from dataframe

MATLAB. How can I sum all the rows of the first column when on the second column the rows have the same value?

How to set column names in R by repeating character?

How can I split a column with multiple values into multiple lines while repeating value from 1st column?

How can I set a matrix with different, repeating patterns in every column?

How can I convert a row from a dataframe in pyspark to a column but keep the column names? - pyspark or python