Re: [R] can not extract rows which match a string

2019-10-03 Thread Richard O'Keefe
I think the problem may lie in your understanding of what "==" does with NA and/or what "[]" does with NA. > x <- c(NA, "Yes") > x == "Yes" [1] NA TRUE Since you say you DON'T want the rows with "Yes", you just want x[is.na(x)] or in your case t11 <- t1[is.na(t1$sex_chromosome_aneuploidy_f22019_0

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Richard O'Keefe
Can we do this very simply? My understanding is that you have a column where all the elements are zero except for perhaps a single one. Consider an example 0 0 1 0 0 where you want -2 -1 0 1 2. This is 1 2 3 4 5 - 3. > v <- c(0,0,1,0,0) > w <- which(v == 1) > a <- seq(along=v) - if (length(w) == 0

Re: [R] can not extract rows which match a string

2019-10-03 Thread Pages, Herve
Hi, On 10/3/19 11:58, Ana Marija wrote: > Hello, > > I have a dataframe (t1) with many columns, but the one I care about it this: >> unique(t1$sex_chromosome_aneuploidy_f22019_0_0) > [1] NA"Yes" > > it has these two values. > > I would like to remove from my dataframe t1 all rows which have

Re: [R] can not extract rows which match a string

2019-10-03 Thread Rui Barradas
Hello again, Sometimes it's better to create indices for each condition and then assemble them with logical operations as needed. i <- t1$sex_chromosome_aneuploidy_f22019_0_0 == "Yes" j <- is.na(t1$sex_chromosome_aneuploidy_f22019_0_0) t1[!i & j, ] j means is.na(.) !i means (.) != "Yes"

Re: [R] can not extract rows which match a string

2019-10-03 Thread Rui Barradas
Hello, Then it's easier, is.na alone will do it. j <- is.na(t1$sex_chromosome_aneuploidy_f22019_0_0) t1[j, ] Hope this helps, Rui Barradas Às 20:29 de 03/10/19, Ana Marija escreveu: Hi Rui, sorry for confusion, I would only need to extract from my t1 dataframe rows which have NA in sex_ch

Re: [R] can not extract rows which match a string

2019-10-03 Thread William Michels via R-help
Hello, I expected the code you posted to work just as you presumed it would, but without a reproducible example--I can only speculate as to why it didn't. In the t1 dataframe, if indeed you only want to remove rows of the t1$sex_chromosome_aneuploidy_f22019_0_0 column which are undefined, you cou

Re: [R] can not extract rows which match a string

2019-10-03 Thread Rui Barradas
Hello, You have to use is.na to get the NA values. t1 <- data.frame(sex_chromosome_aneuploidy_f22019_0_0 = c(NA, "Yes"), other = 1:2) i <- t1$sex_chromosome_aneuploidy_f22019_0_0 == "Yes" & !is.na(t1$sex_chromosome_aneuploidy_f22019_0_0) i t1[i, ] Hope this helps, Rui Bar

[R] can not extract rows which match a string

2019-10-03 Thread Ana Marija
Hello, I have a dataframe (t1) with many columns, but the one I care about it this: > unique(t1$sex_chromosome_aneuploidy_f22019_0_0) [1] NA"Yes" it has these two values. I would like to remove from my dataframe t1 all rows which have "Yes" in t1$sex_chromosome_aneuploidy_f22019_0_0 I tried

Re: [R] stats::lm has inconsistent output when adding constant to dependent variable

2019-10-03 Thread Aleš Žiberna
In one case they are exactly 0 and in the other they are almost zero. This is the reason for different results. Of course, they should be exactly the same, but this is due to some integer values not being exactly represented as real values on binary computers. Best, Aleš Žiberna On Fri, Sep 27,

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Thank you very much for your help! All the best, Faradj > 3 okt. 2019 kl. 16:37 skrev Eric Berger : > > You can replace the last line in my first suggestion by the following two > lines > > d <- 2014 # the default (set by the user) > a$treatment <- sapply( 1:nrow(a), function(i) { b <- v[a$

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
You can replace the last line in my first suggestion by the following two lines d <- 2014 # the default (set by the user) a$treatment <- sapply( 1:nrow(a), function(i) { b <- v[a$country_code[i]]; a$year[i] - ifelse(is.na(b),d,b)}) Best, Eric On Thu, Oct 3, 2019 at 5:19 PM Faradj Koliev wr

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Hi, I was thinking that it could simply show the negative counts. For ex: if a country hasn’t introduced the policy X, and it's in the dataset from 1982 to 2014, then the treatment variable would take a value -33 in 1982 and -1 in 2014. Best, Faradj > 3 okt. 2019 kl. 16:11 skrev Eric Ber

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
Hi Faradj, What should the treatment variable be in those cases? If you want to set it to a constant y (such as y=0), you can add something like y <- 0 a$treatment[ is.na(a$treatment) ] <- y HTH, Eric On Thu, Oct 3, 2019 at 4:54 PM Faradj Koliev wrote: > Dear Eric, > > Thank you very much for

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear Eric, Thank you very much for this - it worked perfectly! A small thing: I wonder whether it’s possible to include those cases where the x is =0 for the whole study period. I have countries with x=0 for the whole period and the treatment variable is=NA for these observations. Best, Fa

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Eric Berger
Hi Faradj, Suppose your data frame is labeled 'a'. Then the following seems to do what you want. v <- rep(NA_integer_,max(a$country_code)) v[ a$country_code[a$x==1] ] <- a$year[a$x==1] a$treatment <- sapply( 1:nrow(a), function(i) { a$year[i] - v[a$country_code[i]]}) HTH, Eric On Thu, Oct 3, 20

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear Michael Dewey, Thanks for reaching out about this. I trying again, now with plain text, and hope it works. Best, Faradj Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how the variable X (a certai

Re: [R] Creating a before-and-after variable in R

2019-10-03 Thread Michael Dewey
Dear Faradj I am afraid your post is unreadable since this is a plain text list and you sent in HTML. Michael On 03/10/2019 12:17, Faradj Koliev wrote: Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how t

[R] Creating a before-and-after variable in R

2019-10-03 Thread Faradj Koliev
Dear R-users, I need an urgent help with the following: I have a country-year data covering the period 1982 - 2013. I want to assess how the variable X (a certain policy) affects the Y variable. The X variable is =1 when a country introduces that policy in a specific year, otherwise =0. What