Subset a data frame based on elements in a list - r

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]) ]

Related

How to use indexing to replace a row of a matrix

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)

Replacing for-loops for Excel-like formula filling in dataframe/matrix

I'm trying to perform basic excel-like formula-filling in R. I want to populate the value of a "cell" based on the values of other cells in the same matrix or data.frame. The function is pretty straightforward to do with a single cell, but seems to be more difficult to scale across both rows and columns.
Say I have a simple matrix:
simple <- matrix(c(0,1,2,3,0,4,5,6,7,NA,NA,NA,8,NA,NA,NA), nrow = 4, ncol = 4)
[,1] [,2] [,3] [,4]
[1,] 0 0 7 8
[2,] 1 4 NA NA
[3,] 2 5 NA NA
[4,] 3 6 NA NA
I want to populate the NAs with the sum of columns 1 and 2 in the same row and row 1 in the same column. In Excel, for cell C2 it would be
=$A2 + $B2 + C$1
in R
simple[2,3] <- simple[2,1] + simple[2,2] + simple[1,3]
In Excel, you can simply drag the formula over the remaining cells, and voila. In R, not so easy.
Since r is vectorized, I can fill a whole column pretty easily by giving ranges instead of single cells, like so:
simple[2:4,3] <- simple[2:4,1] + simple[2:4,2] + simple[1,3]
[,1] [,2] [,3] [,4]
[1,] 0 0 7 8
[2,] 1 4 12 NA
[3,] 2 5 14 NA
[4,] 3 6 16 NA
But when I try to vectorize over both rows and columns, it doesn't work because it interprets the last value as the vector c(7,8), and tries to add that in a row-wise fashion, rather than adding it column-wise.
simple[2:4,3:4] <- simple[2:4,1] + simple[2:4,2] + simple[1,3:4]
Warning message:
In simple[2:4, 1] + simple[2:4, 2] + simple[1, 3:4] :
longer object length is not a multiple of shorter object length
[,1] [,2] [,3] [,4]
[1,] 0 0 7 8
[2,] 1 4 12 12
[3,] 2 5 15 15
[4,] 3 6 16 16
As an alternative solution, one could do nested for loops, as below:
for (i in 2:4){
for (j in 3:4){
simple[i,j] <- simple[i,1] + simple[i,2] + simple[1,j]
}
}
[,1] [,2] [,3] [,4]
[1,] 0 0 7 8
[2,] 1 4 12 13
[3,] 2 5 14 15
[4,] 3 6 16 17
This actually works and is pretty easy, but it involves nested for loops, so, enough said.
I feel like the "right" solution would be one using correct vectorization, apply(), or dplyr, but I can't seem to figure out how to make them work, short of rearranging the data from a crosstab format to a flat format, but that can explode your file size pretty quickly.
Any ideas on how to make this work in a more R-ish fashion?
Here's a more R like way to do it, let's convert simple to a data.frame first.
library(tidyverse)
df1 <- as.data.frame(simple)
df1 %>% mutate(V3 = V1 + V2 + first(V3), V4 = V1 + V2 + first(V4))
V1 V2 V3 V4
1 0 0 7 8
2 1 4 12 13
3 2 5 14 15
4 3 6 16 17
first from dplyr is handy because it lets you lock to the first value in the column, like you would in Excel with C$1
In matrix arithmetic, each component must be same dimension or any being a single-item vector. Therefore, consider aligning by replicating 7 and 8 for each needed row 2-4 (i.e., 3 times). Then transpose for 2 X 3 dimension:
simple[2:4,3:4] <- simple[2:4,1] + simple[2:4,2] + t(replicate(length(2:4), simple[1,3:4]))
Alternatively, consider sapply iterating through 7 and 8 values respectively:
simple[2:4,3:4] <- sapply(3:4, function(i) simple[2:4,1] + simple[2:4,2] + simple[1,i])
Slightly more concise with rowSums and leaving out row indexing:
simple[,3:4] <- sapply(3:4, function(i) rowSums(simple[,1:2]) + simple[1,i])
I may be late to the game but here is a data.table and base R solution which for large data sets is much faster than tidyverse. The syntax may look more confusing at first but breaking it down piece by piece is very logical and straight-forward once you have a good handle on lapply.
To make the cell and the vectors you are adding compatible you should convert the cell to a vector by simply replicating that value as many times as the number of observations or rows of the dataframe. So in your example, V3 = rep(7,4) will yield a vector with all 7s. R will then let you do V3=V1+V2+V3, where V3 on the right-hand side is the rep(7,4).
The data.table has some handy built-in special read-only symbols that will also give you the ability to extend the solution beyond the two columns you provided in the example. The two I use most frequently are .SD and .N. In this example, you can think of .SD as a way to refer to all columns except the first two and .N is always a constant number equal to the number of rows in the data.table. These symbols can be used in the j slot of a data.table which is equivalent to the columns of a matrix or data.frame object. So your code would look like this:
simple <- data.table(simple)
NAcols <- colnames(simple)[-c(1,2)] ##Can modify this to get names of columns you wish to change if its not the first two using match or grep. I can add that if you want?
simple[,NAcols:=lapply(.SD,function(i) V1+V2+rep(i[1],.N)),.SDcols=NAcols]
Note that each iteration in the lapply loop is simply the ith column and i[1] selects only the first element of that column and replicates it as many times as the number of rows (.N) before adding the three vectors together. The .SDcols is used to prevent this function from being applied to the first two columns. Though there was no need in this problem to group, data.table also allows you to specify 'by = ' as an argument if you want to group by a particular column or columns in the data.table before applying the function. Finally note that I did not need to assign the last line of code to another R object because data.table updates the old columns of 'simple' using pointers which is why it is so much faster than base R and tidyverse data frame objects. However you can use the copy function of data.table like this instead if you wish to save the original data.table for some reason:
final_result <- copy(simple)[,NAcols:=lapply(.SD,function(i) V1+V2+rep(i[1],.N)),.SDcols=NAcols]
Anyway I hope that explanation helps and if you need me to clarify anything please let me know! Best of luck!

how do I remove duplicate rows from a data frame in R where different permutations across several columns are counted only once? [duplicate]

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))), ]

Using vector type in R data frame

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.

Is it possible to select a column while keeping its colname? [duplicate]

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.

Resources