subset

Retain only rows present in all matrices

若如初见. 提交于 2019-12-10 21:22:44
问题 Suppose one has a list of matrix objects of different lengths, but some row names in common. How could one retain only those rows present in all matrices? I.e., starting with this: > my.list $matrix.a X1 X2 X3 row.A 59 36 9 row.B 54 29 44 row.C 59 36 9 row.D 54 88 32 $matrix.b X1 X2 X3 row.B 47 12 2 row.C 11 36 9 row.D 54 23 99 $matrix.c X1 X2 X3 row.A 95 31 77 row.B 63 29 44 row.C 60 43 2 And producing this: > my.list.subsetted $matrix.a X1 X2 X3 row.B 54 29 44 row.C 59 36 9 $matrix.b X1 X2

Faster way to subset data table instead of a for loop R

痞子三分冷 提交于 2019-12-10 19:01:09
问题 I have a data table (you'll need the data table package installed) in R generated with X and Y coordinates and random data values from both normal and uniform distributions. The coordinates represent points on a 2000x1600 array and has to be divided into 16 smaller "sectors" each 500x400. These sectors need their mean of Normal Distribution values taken, divided by the min^2 of the Uniform Distribution values. I also created two variables x and y using a provided function startstop, that have

R: make 2 subset vectors so that values are different index-wise

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 18:26:04
问题 I want to make 2 vectors subsetting from the same data, with replace=TRUE . Even if both vectors can contain the same values, they cannot be the same at the same index position. For example: > set.seed(1) > a <- sample(15, 10, replace=T) > b <- sample(15, 10, replace=T) > a [1] 4 6 9 14 4 14 15 10 10 1 > b [1] 4 3 11 6 12 8 11 15 6 12 > a==b [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE In this case, vectors a and b contain the same value at index 1 (value==4), which is wrong

Ordering a dataframe by its subsegments

我们两清 提交于 2019-12-10 17:32:01
问题 My team and I are dealing with many thousands of URLs that have similar segments. Some URLs have one segment ("seg", plural, "segs") in a position of interest to us. Other similar URLs have a different seg in the position of interest to us. We need to sort a dataframe consisting of URLs and associated unique segs in the position of interest, showing the frequency of those unique segs. Here is a simplified example: url <- c(1, 3, 1, 4, 2, 3, 1, 3, 3, 3, 3, 2) seg <- c("a", "c", "a", "d", "b",

Lazy evaluation for S4 method arguments

你。 提交于 2019-12-10 17:29:14
问题 I'm implementing an S4 class that contains a data.table , and attempting to implement [ subsetting of the object (as described here) such that it also subsets the data.table . For example (defining just i subsetting): library(data.table) .SuperDataTable <- setClass("SuperDataTable", representation(dt="data.table")) setMethod("[", c("SuperDataTable", "ANY", "missing", "ANY"), function(x, i, j, ..., drop=TRUE) { initialize(x, dt=x@dt[i]) }) d = data.table(a=1:4, b=rep(c("x", "y"), each=2)) s =

How to find the indices of an R list meeting multiple criteria

旧城冷巷雨未停 提交于 2019-12-10 17:19:33
问题 Suppose I have the following data frame in R: set.seed(5) PosActions <- c("Work","Pause","Clockin","Clockout","Lunch") df <- data.frame(ID = c(rep(1,3),rep(2:3,each=4),rep(4,5)), ACTION = sample(PosActions,16,replace=T)) Which returns ID ACTION 1 1 Pause 2 1 Clockout 3 1 Lunch 4 2 Pause 5 2 Work 6 2 Clockout 7 2 Clockin 8 3 Lunch 9 3 Lunch 10 3 Work 11 3 Pause 12 4 Clockin 13 4 Pause 14 4 Clockin 15 4 Pause 16 4 Pause In this data frame, the rows corresponding to ID == 2 and ID == 3 (rows 4

Subset data frame with matrix of logical values

99封情书 提交于 2019-12-10 15:41:19
问题 Problem I have data on two measures for four individuals each in a wide format. The measures are x and y and the individuals are A, B, C, D . The data frame looks like this d <- data.frame(matrix(sample(1:100, 40, replace = F), ncol = 8)) colnames(d) <- paste(rep(c("x.", "y."),each = 4), rep(LETTERS[1:4], 2), sep ="") d x.A x.B x.C x.D y.A y.B y.C y.D 1 56 65 42 96 100 76 39 26 2 19 93 94 75 63 78 5 44 3 22 57 15 62 2 29 89 79 4 49 13 95 97 85 81 60 37 5 45 38 24 91 23 82 83 72 Now, would I

Select Max from each Subset

我只是一个虾纸丫 提交于 2019-12-10 14:42:03
问题 I'm banging my head here. I feel pretty stupid because I'm sure I've done something like this before, but can't for the life of me remember how. One of those days I guess >.< Say I have the following data: ---> and a query which returns this: ---> But I want this: ID FirstID ID FirstID ID FirstID -- ------- -- ------- -- ------- 1 1 1 1 7 1 2 1 3 3 3 3 3 3 4 4 6 4 4 4 5 5 5 5 5 5 6 4 7 1 Notice that my query returns the records where ID = FirstID, but I want it to return the Max(ID) for each

Remove all rows of a category if one row meets a condition

怎甘沉沦 提交于 2019-12-10 14:26:21
问题 Problem: I want to remove all the rows of a specific category if one of the rows has a certain value in another column (similar to problems in the links below). However, the main difference is I would like it to only work if it matches a criteria in another column. Making a practice df prac_df <- data_frame( subj = rep(1:4, each = 4), trial = rep(rep(1:4, each = 2), times = 2), ias = rep(c('A', 'B'), times = 8), fixations = c(17, 14, 0, 0, 15, 0, 8, 6, 3, 2, 3,3, 23, 2, 3,3) ) So my data

Assignment to subset of a matrix with repeated indices

…衆ロ難τιáo~ 提交于 2019-12-10 14:25:54
问题 Not sure this qualifies for an entry in the R-Inferno, but can someone comment on the logic behind the way the following replacement works? foo<-matrix(1:6,2) bar<-foo[2,c(1,3,1)] bar # [1] 2 6 2 foo[2,c(1,3,1)]<-foo[2,c(1,3,1)]+5 foo # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 7 4 11 My question is: when generating bar , the repeated coordinate results in a repeated element in the output, but when modifying foo , the repeated coordinate does not result in a repeated addition operation. (By