This question already has an answer here:
Select matrix column (resp. row) as Nx1 (resp. 1xD) matrix, as opposed to vector
(1 answer)
Closed 9 years ago.
I would like to index-select a column from a matrix while keeping is colname. For example
m<-matrix(1:9,ncol=3)
colnames(m)<-c('V1','V2','V3')
selected<-as.matrix(m[,1])
However,
> selected
[,1]
[1,] 1
[2,] 2
[3,] 3
I would like to have colname(selected)<-'V1' as a result instead. Why does R behave like this and how can I fix it? Thanks.
Remove the as.matrix() in the last line and use drop=FALSE (see ?Extract)
> m<-matrix(1:9,ncol=3)
> colnames(m)<-c('V1','V2','V3')
> m[,1,drop=FALSE]
V1
[1,] 1
[2,] 2
[3,] 3
What you do, is selecting a single column. R will by default drop all dimensions (and hence also the names) that are not necessary. In this case, you drop one dimension as a single column can be seen as a vector. The argument drop=FALSE prevents this.
Related
This question already has answers here:
Is there a way to select all elements of a dimension when matrix-indexing a multidimensional array in R?
(1 answer)
Subset an array using a vector of indices
(1 answer)
Closed 1 year ago.
Let's assume we've a 3-dimensional array like
a <- array(1:24, dim = c(4, 3, 2))
Accessing or indexing an array is usually done via
# fixing the third (=last) dimension and output the corresponding values of the first and second dimension
a[, , 1]
> a
, , 1
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
In this example, I know the number of dimensions is three. So, I can manually type two commas followed by a number for the last dimension (here 1). But now I want to create an expression for dynamically indexing an array by fixing the last dimension a priori not knowing the number of dimensions. This construct should later be used within a loop e.g. lapply().
dims <- paste0(paste0(rep("", length(dim(a)) - 1L), collapse = ","), ",idx")
> dims
[1] ",,idx"
Am I able to convert this dynamically created string ",,idx" into whatever - I know that indexing by itself isn't strongly an expression, that can be used for indexing?
a[dims] <- ... # won't work!
Thanks in advance!
Here is a way, but I'm not sure it's the way you want:
write.table(paste0("a[",dims,"]"),"code.R",row.names = FALSE,col.names = FALSE,quote=FALSE)
source("code.R")$value
This will create a separate R script which contains just the a[,,idx] and then run that script to return the value.
I would like to substitute a one-row vector for some of the rows of a matrix in R. Here is an example.
I would like to substitute the row "5,6" for the rows in A where the entries are 1. So, I would like to make "A" look like "A_goal"
The method I attempted (see the bottom line) was close, but it seems that it's writing "down the columns" instead of across the rows.
A=matrix(c(1,2,1,3,1,2,1,3),4,2)
B=matrix(c(5,6),1,2)
A_goal = matrix(c(5,2,5,3,6,2,6,3),4,2)
A
B
A_goal
# Here is an attempt that didn't work:
A[A==1]=B
A
Matrix indexing using {<- is done with column major ordering. So you will probably need to use apply on a row basis. This is essentially a for-loop over the rows of A. You also will need to transpose since apply will also deliver the results as columns:
t(apply(A, 1, function(x) if(x[1]==1){B}else{x}))
[,1] [,2]
[1,] 5 6
[2,] 2 2
[3,] 5 6
[4,] 3 3
If you were only intending the replacement to occur where the row was c(1,1) then the logical test would need to be modified to x == c(1,1)
This question already has an answer here:
Removing duplicate combinations (irrespective of order)
(1 answer)
Closed 4 years ago.
My problem seems to be very simple but I'm not able to solve it since hours…
I have a matrix such this one:
[,1] [,2]
[1,] 1 2
[2,] 2 1
[3,] 2 1
[4,] 3 4
I want to select the rows which have the same information, without regard to the order of the column.
For instance row1 (1;2) and row2 (2;1). Then, i want to delete them, except one.
I have written this function, but it doesn't work…
f<-function(x){
i<-1
repeat
{
a<-c()
a<-c(which(x[i,1]==x[,2] & x[i,2]==x[,1]))
if(!is.null(a)) {x<-x[-c(a),]}
if(i>=nrow(x)) {break} else {i<-i+1}
}
x
}
f(data)
Somebody could give me a hint for this ?
Like this:
unique(t(apply(mat, 1, sort)))
Note that output rows are sorted, so for example an "unmatched" row like c(5, 1) in the original data will appear as c(1, 5) in the output. If instead you want the output rows to be as they are in the input, then you can do:
mat[!duplicated(t(apply(mat, 1, sort))), ]
This seems like a simple thing to do and I was wondering what the most efficient way to do this was. Suppose I have a dataframe "data" and another frame that has the indices that I want to extract.
data<-as.data.frame(matrix(1:9,3,3))
idx<-cbind(c(1,3,2),c(2,3,1))
is there an easy way to get the elements pointed to by idx assuming the first row is row id and the second row is column id?
I tried data[idx[,1],idx[,2]] hoping that would work but that didn't.
[,1] [,2] element
[1,] 1 2 4
[2,] 3 3 9
[3,] 2 1 2
I am trying to get the element list in a separate list or vector.
Try instead:
data[ cbind( idx[,1],idx[,2]) ]
The R language doesn't allow vectors to be variables. Why it is missing the feature? it would be nice my data frame with following features have something like this:
X1 X2
1. [1,2,3] [2,3,4] <br>
2. .... ....
I tried df <- as.data.frame(c(1,2,3),c(1,2,3)) but keep getting 3 rows created with numeric type instead I want a single row with vector type
Use an array:
array(rbind(1:3, 2:4), dim = c(1, 2, 3))
#, , 1
#
# [,1] [,2]
#[1,] 1 2
#
#, , 2
#
# [,1] [,2]
#[1,] 2 3
#
#, , 3
#
# [,1] [,2]
#[1,] 3 4
R does not allow to work with row data types u can have column data types. Each column can be a separate data type vector. You are not thinking the 'R' way. This short coming as you are putting it, is the strength of R. You can use an Array as suggested in the previous answer, if you strictly want to follow your chain of thought. Or just try to see how you can do what you want to do in 'R'. Rather than try to impose your known language to R.