subset

Subsetting a unbalanced panel dataset to have at least 2 consecutive observations in R

扶醉桌前 提交于 2020-01-03 17:25:42
问题 I have an unbalanced panel dataset in R. The following will serve as an example: dt <- data.frame(name= rep(c("A", "B", "C"), c(3,2,3)), year=c(2001:2003,2000,2002,2000:2001,2003)) > dt name year 1 A 2001 2 A 2002 3 A 2003 4 B 2000 5 B 2002 6 C 2000 7 C 2001 8 C 2003 Now, I need to have at least 2 consecutive year observations for each name . Hence, I would like to remove row 4, 5, and 8. How do I best do that in R? EDIT: Thanks to the comment below, I can make a bit clearer. If I had an

When subsetting rows with a factor with equal (==), NA's are also included. It doesn't happen with %in%. Is it normal?

笑着哭i 提交于 2020-01-03 11:49:52
问题 Suppose I have a factor A with 3 levels A1, A2, A3 and with NA's. Each appears in 10 cases, so there is a total of 40 cases. If I do subset1 <- df[df$A=="A1",] dim(subset1) # 20, i.e., 10 for A1 and 10 for NA's summary(subset1$A) # both A1 and NA have non-zero counts subset2 <- df[df$A %in% c("A1"),] dim(subset2) # 10, as expected summary(subset2$A) # only A1 has non-zero count And it is the same whether the class of the variable used for subsetting is factor or integer. Is it just how equal

When subsetting rows with a factor with equal (==), NA's are also included. It doesn't happen with %in%. Is it normal?

◇◆丶佛笑我妖孽 提交于 2020-01-03 11:49:04
问题 Suppose I have a factor A with 3 levels A1, A2, A3 and with NA's. Each appears in 10 cases, so there is a total of 40 cases. If I do subset1 <- df[df$A=="A1",] dim(subset1) # 20, i.e., 10 for A1 and 10 for NA's summary(subset1$A) # both A1 and NA have non-zero counts subset2 <- df[df$A %in% c("A1"),] dim(subset2) # 10, as expected summary(subset2$A) # only A1 has non-zero count And it is the same whether the class of the variable used for subsetting is factor or integer. Is it just how equal

Subsetting in H2O R

别来无恙 提交于 2020-01-03 08:13:10
问题 I have a h2o object. The standard R for subset sub1<-trans[trans$Type==1,] I tried the same in h2o. It is not working sub1<-trans[trans$Type==1,] I also tried sub1<-h2o.exec(trans[trans$Type==1,]) note* trans is a h2o data Object. Any idea to do it in h2o? Thanks 回答1: I'm not sure if this is the most "hydrophilic" way to do this but: transType <- trans$Type sub1 <- trans[transType == 1,] Seems to work for me with no problem. For a more reproducible example, consider library(h2o) localH2O <-

Subsetting neighboring fileds

梦想与她 提交于 2020-01-03 05:33:17
问题 I am trying to make a conditional subsetting which would include contemporary elements inside window of neighboring areas. For example, given the matrix Dat, where Species (SP), Area (AR) and Time (TM): SP AR TM A 2 2 B 2 2 C 1 4 F 3 2 B 5 3 E 3 2 D 2 1 I 1 4 H 3 2 E 2 4 D 3 5 B 1 2 How can I retrieve all the species co-occurring with species A in the same time, but within neighboring areas (in this case 1 and 3)? The desired output being: SP AR TM A 2 2 B 2 2 F 3 2 H 3 2 B 1 2 This is based

Working with substitute after `$` sign in R

痞子三分冷 提交于 2020-01-03 04:45:08
问题 I was wondering if there is a way that G in my code below could work after $ just like D$post == 1 works? D <- data.frame(post = 1:10, out = 2:11) G <- substitute(post == 1) D$G ## can we make `G` to work like `D$post`? D$post == 1 ## Works 回答1: You can do this: G <- substitute(post == 1) E <- substitute(D$G, list(G = G)) #D$post == 1 That expression looks like what you want, right? Well, it isn't, as you can see when you try to evaluate it: eval(E) #Error in D$post == 1 : invalid subscript

how to make all possible power set(or subset) from arrayList objects?

試著忘記壹切 提交于 2020-01-02 22:00:53
问题 Say I have the following class: class A { String name; Double value; } and a list of the above class objects which might have: [{f 2.1}, {c 1.1}, {a 0.3}... and so on] [{n 0.5}, {f 1.9}, {x 0.1}, {a 1.9}, {b 1.1}... and so on] ... and so on all I want is to do the followings: 1. Building power subsets from the internal list items(N.B: skip the single subsets). 2. Push the subset in another List as an object of the above class A like this: a. if f,c is a subset of 1st element then f,c would be

how to make all possible power set(or subset) from arrayList objects?

冷暖自知 提交于 2020-01-02 22:00:11
问题 Say I have the following class: class A { String name; Double value; } and a list of the above class objects which might have: [{f 2.1}, {c 1.1}, {a 0.3}... and so on] [{n 0.5}, {f 1.9}, {x 0.1}, {a 1.9}, {b 1.1}... and so on] ... and so on all I want is to do the followings: 1. Building power subsets from the internal list items(N.B: skip the single subsets). 2. Push the subset in another List as an object of the above class A like this: a. if f,c is a subset of 1st element then f,c would be

extracting rows of group medians n R

℡╲_俬逩灬. 提交于 2020-01-02 09:54:20
问题 If I have a data frame like the following: v2 <- c(4.5, 2.5, 3.5, 5.5, 7.5, 6.5, 2.5, 1.5, 3.5) v1 <- c(2.2, 3.2, 1.2, 4.2, 2.2, 3.2, 2.2, 1.2, 5.2) lvl <- c("a","a","a","b","b","b","c","c","c") d <- data.frame(v1,v2,lvl) > d v1 v2 lvl 1 2.2 4.5 a 2 3.2 2.5 a 3 1.2 3.5 a 4 4.2 5.5 b 5 2.2 7.5 b 6 3.2 6.5 b 7 2.2 2.5 c 8 1.2 1.5 c 9 5.2 3.5 c Within each level of d$lvl , I want to extract the row with value of d$v1 being median (for the simplest case, each level of d$lvl has three rows). So I

Coq case analysis and rewrite with function returning subset types

混江龙づ霸主 提交于 2020-01-02 07:14:04
问题 I was working is this simple exercise about writing certified function using subset types. The idea is to first write a predecessor function pred : forall (n : {n : nat | n > 0}), {m : nat | S m = n.1}. and then using this definition give a funtion pred2 : forall (n : {n : nat | n > 1}), {m : nat | S (S m) = n.1}. I have no problem with the first one. Here is my code Program Definition pred (n : {n : nat | n > 0}) : {m : nat | S m = n.1} := match n with | O => _ | S n' => n' end. Next