Select column and row values with an index in data frame

<>

This question already has an answer here:

Answers


Since you have your indices in ind one way could be to use mapply:

#the first argument is the function call 
#second argument is your matrix coerced to data.frame
#third argument is your indices
#each time an index will be used in conjunction to a column
#and you get your result
mapply(function(x,y) x[y], as.data.frame(mat), ind)
#       LO3         Tx         Gh         RH         SR 
# 0.4282915  0.4863608 -0.4962140 -0.8023141 -0.8023141 

This can do it for you:

mapply(function(i,j) sample[i,j], seq_len(ncol(sample)), ind)

> mapply(function(i,j) sample[i,j], seq_len(ncol(sample)), ind)
[1]  0.4282915  0.4863608 -0.4962140 -0.8023141 -0.8023141

if you want you can set the names of the result from ind


Need Your Help

Mutex dynamic allocated

c pthreads mutex

I sucessfully used a static mutex, but i got problem with dynamic version.

How to use sed to pick up a specific paragraph and get rid of repeated ones

awk sed paragraphs

I want to pick up the lines begin with '---------read-------' and end with 'finish.' from a log file showing below, at the meantime, get rid of the repeated paragraphs (keep only the last match of ...