How do I transform a row vector like the one above (1x3)
a = [1 2 3];
In to a column vector?
Found it! It's vec(). The reason why I couldn't find it is because I kept on looking for "row vector". Instead it's a 2D array.
Simply use vec(a) and it will be a standard column vector.
Related
This seems to be simple but I can't find the answer.
I combine two vectors using cbind().
> first = c(1:5)
> second = c(6:10)
> values = cbind(first,second)
When I want to retrieve a single element using values[1,2] I always get the column name in addition to the actual element.
> values[1,2]
second
6
How can I get the value without the column name?
I know I can remove the column names in the matrix like in this post: How to remove column names from a matrix in R? But how can I leave the matrix as is and only get the value I want?
We can use unname
unname(values[1,2])
#[1] 6
Or as.vector
as.vector(values[1,2])
You can use the [[ operator to extact a single element,
values[[1,2]]
# [1] 6
I want to know if a vector is 1xN or Nx1 in R. What function should I use? Length returns only one value regardless of the vector type.
As AnandaMahto's comment, using NROW/NCOL it returns numbers of rows and columns.
In R, vectors don't have dimensions. The dimension of a vector is NULL. Whereas, arrays, matrices, data frames, tables have dimensions.
If you want to know the value of N(that is the number of elements in a vector) you can use the length function
I have another problem in R. I need to combine four vectors into one vector. Imagine four vectors of the same length, and I wish to combine like: first element of vector a multiply by first element of vector b plus first element of vector c multiply by first element of vector d.
Here is what I have tried:
x<-rep(5,5)
a<-seq(1,5,1)
c<-rep(1,5)
d<-rep(2,5)
div<-NULL
for(i in 1:5){
div[i]<-x[i]*a[i]+c[i]*d[i]
div<-rbind(div[i])
}
div
[,1]
[1,] 27
I really think that the outcome of this loop should be a vector, but my outcome is just a number. What am I doing wrong?
Yes, div<-rbind(div[i]) is wrong, should not have been there at all because it overwrites all your previously computed data. If you remove it, the result will probably be correct, but you can instead just perform a vectorized operation and not need a loop, like this:
div <- a * x + c * d
This will perform the computation on each set of values of those 4 vectors; the result will a new vector with what you wanted to accomplish as a result.
I have a 4 column array, I would like to obtain a vector containing for each row the label of the column which contained the maximum value for this row.
I can do this in loops but I would like to use matrix functions for speed.
How can I do this without programming my own lib functions ?
There is a function that does just this. If x is your matrix, try max.col(x).
I am trying to remove duplicated rows by one column (e.g the 1st column) in an R matrix. How can I extract the unique set by one column from a matrix? I've used
x_1 <- x[unique(x[,1]),]
While the size is correct, all of the values are NA. So instead, I tried
x_1 <- x[-duplicated(x[,1]),]
But the dimensions were incorrect.
I think you're confused about how subsetting works in R. unique(x[,1]) will return the set of unique values in the first column. If you then try to subset using those values R thinks you're referring to rows of the matrix. So you're likely getting NAs because the values refer to rows that don't exist in the matrix.
Your other attempt runs afoul of the fact that duplicated returns a boolean vector, not a vector of indices. So putting a minus sign in front of it converts it to a vector of 0's and -1's, which again R interprets as trying to refer to rows.
Try replacing the '-' with a '!' in front of duplicated, which is the boolean negation operator. Something like this:
m <- matrix(runif(100),10,10)
m[c(2,5,9),1] <- 1
m[!duplicated(m[,1]),]
As you need the indeces of the unique rows, use duplicated as you tried. The problem was using - instead of !, so try:
x[!duplicated(x[,1]),]