In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively inefficient. Is there a more direct way to do it?
For bonus points, is there a function that works if x is a vector? That is, it should return a vector of indices indicating the position of each element of x in v.
its not an difficult task
I have a square matrix, and I'm trying to:
Change only the off-diagonal elements
Apply a function to each off-diagonal element based on the identity of the element's row and column, using a separate list of values of length row/column.
I want to multiply each element of the matrix by row number element of the list, and olumn number element of the list.
Here's the matrix and the list from which I want my function to draw values to multiply for each matrix element.
states <- matrix(seq(1,9,1), nrow=3,ncol=3)
print(states)
rates <- seq(1, 5,2)
print(rates)
So for example, I want to multiply 8 in the matrix by 3*5 since it is in the 2nd row and 3rd column.
I've indexed the off-diagonal elements (not sure if this was the best way for my purposes):
delta <- row(states)-col(states)
And tried to sapply:
sapply(states[delta>0],function(i) {
i*rates[row(i)]*rates[col(i)]
print(states)
})
But I get the error message " Error in row(i) : a matrix-like object is required as argument to 'row' ". I know it's a problem with how I'm indexing the elements but can't think of a better way.
Thanks in advance!
Figured out the answer!
The problem arose from trying to pick out a single value in the matrix, which doesn't work for functions row() and col().
So rather than indexing as
rates[row(i)]
it needed to be
rates[row(states)[i]]
I have a matrix eval_matrix which has dimensions (200,45). I want to get the most occurring digit in each row and want to make a new matrix maj of 200 rows and one column.
I am trying this:
maj=c()
for (i in nrow(eval_matrix)){
m=names(which.max(table(eval_matrix[i,])))
m<-as.numeric(m)
maj<-rbind(maj,m)
}
View(maj)
But it is only giving me the last row's result in my new matrix maj.
What's going wrong here?
You can also use apply() over each row of a matrix and turn the result into a one-column matrix. In a single line:
matrix(as.numeric(apply(eval_matrix, 1, function(x) names(which.max(table(x))))), ncol=1)
I am new in R and probably this is an easy question:
I have the following vector:
P <- c(23,45,98)
These values represent the numbers of rows
Now, I have a table with only one column and I would like to obtain the values on each row from the previous vector and return it into 3 different objects (Variables).
e.g. The row #23 has the value P05.14 and for this first value of the vector "P" I want to create a variable or object like: A = P05.14. The same with the other two values of that vector.
Thanks for your help.
If you only have the three values, just do it manually:
A <- dat[23,]
B <- dat[45,]
C <- dat[98,]
For more values, you can assign them in a loop:
for(value in P){
assign(paste0("A",value), as.character(dat[value,]))
}
I should note that in a situation such as this, it would be best to use a list, and not litter the workspace with variable. But to each their own. Good luck!
What if one wants to apply a functon i.e. to each row of a matrix, but also wants to use as an argument for this function the number of that row. As an example, suppose you wanted to get the n-th root of the numbers in each row of a matrix, where n is the row number. Is there another way (using apply only) than column-binding the row numbers to the initial matrix, like this?
test <- data.frame(x=c(26,21,20),y=c(34,29,28))
t(apply(cbind(as.numeric(rownames(test)),test),1,function(x) x[2:3]^(1/x[1])))
P.S. Actually if test was really a matrix : test <- matrix(c(26,21,20,34,29,28),nrow=3) , rownames(test) doesn't help :(
Thank you.
What I usually do is to run sapply on the row numbers 1:nrow(test) instead of test, and use test[i,] inside the function:
t(sapply(1:nrow(test), function(i) test[i,]^(1/i)))
I am not sure this is really efficient, though.
If you give the function a name rather than making it anonymous, you can pass arguments more easily. We can use nrow to get the number of rows and pass a vector of the row numbers in as a parameter, along with the frame to be indexed this way.
For clarity I used a different example function; this example multiplies column x by column y for a 2 column matrix:
test <- data.frame(x=c(26,21,20),y=c(34,29,28))
myfun <- function(position, df) {
print(df[position,1] * df[position,2])
}
positions <- 1:nrow(test)
lapply(positions, myfun, test)
cbind()ing the row numbers seems a pretty straightforward approach. For a matrix (or a data frame) the following should work:
apply( cbind(1:(dim(test)[1]), test), 1, function(x) plot(x[-1], main=x[1]) )
or whatever you want to plot.
Actually, in the case of a matrix, you don't even need apply. Just:
test^(1/row(test))
does what you want, I think. I think the row() function is the thing you are looking for.
I'm a little confuse so excuse me if I get this wrong but you want work out n-th root of the numbers in each row of a matrix where n = the row number. If this this the case then its really simple create a new array with the same dimensions as the original with each column having the same values as the corresponding row number:
test_row_order = array(seq(1:length(test[,1]), dim = dim(test))
Then simply apply a function (the n-th root in this case):
n_root = test^(1/test_row_order)