1

i have a dataframe with 25000 rows and 51 columns.

head(df) here ..... means n1 .....n51

chr1_12750000_12760000  2   8   7   3   5   7   15  11 .......  10
chr11_11280000_11290000 8   6   7   8   7   12  10  10 .......  10
chr9_21700000_21710000  2   2   0   0   2   3   1   0 .......   0
chr1_162500000_162510000    0   2   2   3   4   3   4   3 .......   4
chr11_53780000_53790000 9   7   4   3   3   2   3   6 .......   8
chrX_157610000_157620000    5   10  5   8   17  13  15  9 .......   11
chr4_134170000_134180000    0   0   1   0   1   0   3   2 .......   2
chr1_62090000_62100000  8   15  15  16  7   9   10  20 .......  6
chr2_16540000_16550000  5   1   3   6   7   10  9   8 .......   0
chr6_57740000_57750000  2   2   1   2   1   2   4   4 .......   5

In R, I'd like to perform the following calculations column per column.

for column n2(i)

  1. sort df in descending order according to n2(i)
  2. find the value at 1250 row
  3. divide all the value of the column with the value at row 1250

repeat above steps for n3 ... n51

Thanks in advance :)

learner
  • 41
  • 4
  • Hi, try reformatting your code using the dput() command in R, that way it will be easier for others to load the matrix onto their own machines and potentially reducing the scale of the problem to the minimum example necessary to answer your question. see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Liam Haller Jun 30 '23 at 09:16

1 Answers1

0

Here is a base R way with a apply loop.

# make up some data
m <- matrix(rnorm(1000*51), ncol = 51)
colnames(m) <- sprintf("n%d", 1:51)
# this should be 1250L
i <- 125L

# here are the wanted divisions
#   divide the columns by the value in the ith (125 or 1250) 
#   row after sorting the column; the result keeps the column
#   in its original order
m[, 2:51] <- apply(m[, 2:51], 2, \(x) {
  j <- order(x)[i]
  x/x[j]
})

Created on 2023-06-30 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66