Shaxi Liver
Shaxi Liver

Reputation: 1120

How to apply a function for every row in data?

My data:

> dput(head(tbl))
structure(c("a2p1u8", "a2qab2", "a6zl23", "a6zlf3", "a6zq61", 
"a6ztx1", "0", "0", "0", "0", "0.9339597", "0", "0", "0", "0", 
"0", "14.2445924", "0", "0", "0", "0", "0", " 1.84391660", "0", 
"0", "0", "0", "0", "1.00000000", "0", "0", "0", "0", "0", "0.85034470", 
"0", "0.06312408", "0", "0", "1.11684073", "1.00000000", "1.29478436", 
"0.135377134", "0", "0", "0.941579636", "0.389199799", "0.705215641", 
"0.34063483", "0", "0", "1.00000000", "0.46785766", "0", "1.43325438", 
"0", "0", "0", "0.15782118", "0", "1.71425096", "0", "0", "0", 
"0.38274080", "0", " 0.71553232", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "1.72759758", "0", "0", "0", "0", "0", "1.712898580", 
"0", "0", "0", "0", "0", "0.74788829", "1.00000000", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "1.29452015", "0", "0", 
"0", "0", "0", "0.85273992", "0", "0", "0"), .Dim = c(6L, 25L
), .Dimnames = list(NULL, c("Gene name", "2_1", "2_2", "2_3", 
"2_4", "2_5", "2_6", "2_7", "2_8", "2_9", "2_10", "2_11", "2_12", 
"2_13", "2_14", "2_15", "2_16", "2_17", "2_18", "2_19", "2_20", 
"2_21", "2_22", "2_23", "2_24")))

As an output I want to get a new data.frame/matrix with the same number of rows and columns and with the number 1 in the cells which this function finds a peak.

which(diff(sign(diff(Gene name)))==-2)+1

How to find a peak in each row of data.frame ?

Upvotes: 0

Views: 149

Answers (1)

jbaums
jbaums

Reputation: 27388

What you're trying to do is probably easiest with a numeric matrix, so we'll take the gene names and store them to use as row names, and we'll remove the gene column. We'll then add the row names and coerce the matrix to numeric (this last step should be done column by column, i.e. applying the as.numeric function over the 2nd dimension.

nm <- tbl[, 1]
tbl <- apply(tbl[, -1], 2, as.numeric)
row.names(tbl) <- nm

Now we can create a binary indicator matrix that shows whether diff(sign(diff(x))) is equal to -2. We do this by applying your function (slightly modified, removing the which call to ensure it returns a matrix of the desired dimensions) to the first dimension (rows) of tbl.

minus2 <- t(apply(tbl, 1, function(x) as.numeric(diff(sign(diff(x)))==-2)))

We want the columns to the right of those that were -2, so we can cbind a column of zeroes to the left minus2.

peaks <- cbind(0, minus2)

This produces:

peaks

#        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
# a2p1u8    0    0    0    0    0    0    0    0    0     1     0     0     0     0     1     0     0     0     0     0     0     0     0
# a2qab2    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0
# a6zl23    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
# a6zlf3    0    0    0    0    0    1    0    1    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
# a6zq61    0    1    0    0    0    1    0    1    0     1     0     0     0     0     0     0     0     0     0     0     0     0     0
# a6ztx1    0    0    0    0    0    1    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

Upvotes: 1

Related Questions