similar to: Using if and else in a data frame

Displaying 20 results from an estimated 20000 matches similar to: "Using if and else in a data frame"

2012 May 16
2
Help needed for efficient way to loop through rows and columns
Dear R-helpers: I am trying to write a script that iterates through a dataframe that looks like this: Example dataset called "sample": names <- c("S1", "S2", "S3", "S4") X <- c("BB", "AB", "AB", "AA") Y <- c("BB", "BB", "AB", "AA") Z <- c("BB",
2018 May 27
1
if else with 4 conditions problem
Hi everyone, I have two columns: ? ?A? ? ? ? ? ? ? ?B ? ?1? ? ? ? ? ? ? ?1? ?1? ? ? ? ? ? ? ?0 ? ?0? ? ? ? ? ? ? ?1 ? ?0? ? ? ? ? ? ? ?0 I have 4 categories which are: 1) if A = 1 and B =1 then A1 = 1, else A2 = 0, A3 = 0, A4 = 0 2) if A = 1 and B =0 then A1 = 0, else A2 =1, A3 = 0, A4 = 0 3) if A = 0 and B = 1 then A1 = 0, else A2 = 0, A3 = 1, A4 = 0 4) if A = 0 and B =0 then A1 = 0, else A2 =
2018 Jan 08
2
Replace NAs in split lists
Thank you Jeff. Your code works, as usual , perfectly. I am just wondering why if i put the whole code in one line, i get an error message. sdf2 <- lapply( sdf, function(z){z$Value <-ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z}) error. unexpected symbol in sdf2 Thanks again EK On Mon, Jan 8, 2018 at 3:12 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: >
2018 Jan 08
1
Replace NAs in split lists
OPS! Sorry i did indeed posted the code in HTML; should have known better. ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z}) error. unexpected symbol in sdf2 On Mon, Jan 8, 2018 at 11:44 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > I don't know. You seem to be posting in HTML so your code is mangled. Can you post plain text and use the reprex package to
2013 Jun 18
1
transform 3 numeric vectors empty of 0/1
Dear all, Without a loop, I would like transform 3 numeric vectors empty of 0/1 of same length Vec1 : transform 1 to A and 0 to "" Vec2 : transform 1 to B and 0 to "" Vec3 : transform 1 to C and 0 to "" to obtain only 1 vector Vec who is the paste of the 3 vectors (Ex : ABC, BC, AC, AB,...) Any idea ? Thank you for your help -- Michel ARNAUD
2018 Jan 08
0
Replace NAs in split lists
I don't know. You seem to be posting in HTML so your code is mangled. Can you post plain text and use the reprex package to make sure it produces the errorin a clean R session? -- Sent from my phone. Please excuse my brevity. On January 8, 2018 8:03:45 AM PST, Ek Esawi <esawiek at gmail.com> wrote: >Thank you Jeff. Your code works, as usual , perfectly. I am just >wondering why
2018 Jan 08
0
Replace NAs in split lists
Upon closer examination I see that you are not using the split version of df1 as I usually would, so here is a reproducible example: #---- df1 <- read.table( text= "ID ID_2 Firist Value 1 a aa TRUE 2 2 a ab FALSE NA 3 a ac FALSE NA 4 b aa TRUE 5 5 b ab FALSE NA ", header=TRUE, as.is=TRUE ) sdf <- split( df1, df1$ID ) # note the extra [ 1 ]
2018 Jan 08
3
Replace NAs in split lists
Why do you want to modify df1? Why not just reassemble the parts as a new data frame and use that going forward in your calculations? That is generally the preferred approach in R so you can re-do your calculations easily if you find a mistake later. -- Sent from my phone. Please excuse my brevity. On January 7, 2018 7:35:59 PM PST, Ek Esawi <esawiek at gmail.com> wrote: >I just came
2012 Nov 06
1
Filling matrix elements with a function
Hi all, I have a matrix simulating migration in a spatial model. I want to be able to define movement (the values of m1, m2 and m3) as only != 0 between adjacent patches and contingent on certain conditions as defined in the function. Here is the code: WET<-function(t) {everglades$precipitation[t]} #simply reads precipitation data from a csv, value is numeric AB<-function(WET,t)
2012 Jun 18
6
Trying to speed up an if/else statement in simulations
Dear R-help, I am trying to write a function to simulate datasets of size n which contain two time-to-event outcome variables with associated 'Event'/'Censored' indicator variables (flag1 and flag2 respectively). One of these indicator variables needs to be dependent on the other, so I am creating the first and trying to use this to create the second using an if/else statement.
2012 May 15
2
Transfering data from R list to other document format
Dear users, I want to transfer a list of results from R to some practical format, from where I can continue manipulating, copying,... the values, e.g. : list1 <- list("My first list", matrix(1:6, ncol=3), c(1,2,3,4,5,6) ) # Imagining I forgot something and want to add it to the list like: list1[[4]] <- list(c(4,4,4), "This is it") # Now I want to transfer the list to
2018 Jan 08
4
Replace NAs in split lists
Hi all-- I stumbled on this problem online. I did not like the solution given there which was a long UDF. I thought why cannot split and l/s apply work here. My aim is to split the data frame, use l/sapply, make changes on the split lists and combine the split lists to new data frame with the desired changes/output. The data frame shown below has a column named ID which has 2 variables a and b;
2018 Jan 08
2
Replace NAs in split lists
Hi With the example, na.locf seems to be the easiest way. > library(zoo) > na.locf(df1) ID ID_2 Firist Value 1 a aa TRUE 2 2 a ab FALSE 2 3 a ac FALSE 2 4 b aa TRUE 5 5 b ab FALSE 5 Cheers Petr > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff > Newmiller > Sent: Monday, January
2008 Jun 04
5
help understanding why #function(x, y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})# doesn't work like I think it should
Hello R users and developers, I am trying to write several functions (fairly new at this) in order to avoid using loops on large data frames (because they are slow). I wrote the function below and it seems to get hung on the first part of the if statement and then applies that condition to rest of the function. So if (x-y) is greater than 0 the function uses the true statement for the
2003 Jul 11
2
troubleshooting help
Hi, I've installed Samba and started up the processes; however, I do not see the Samba server in Network Neighborhood. In using the DIAGNOSIS.txt file, I cannot get past test 3: ./smbclient -U% -L localhost. The output is as follows: added interface ip=140.188.120.55 bcast=140.188.120.255 nmask=255.255.255.0 session setup failed: Call returned zero bytes (EOF) Any insight into this
2018 Jan 08
2
Replace NAs in split lists
You can enforce these assumptions by sorting on multiple columns, which leads to na.locf(df1[ order(df1$ID,df1$Value), ]) On Mon, Jan 8, 2018 at 4:19 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > Yes, you are right if the IDs are always sequentially-adjacent and the > first non-NA value appears in the first record for each ID. > -- > Sent from my phone. Please
2004 Feb 27
2
question about if else
Today is a good day for asking question, I guess. > c() NULL > > length(c())==0 [1] TRUE > > r = ifelse(length(c())!=0, c(), c(1,2)) ### OK > r = c() ### OK > r = ifelse(length(c())==0, c(), c(1,2)) ### why this is not OK (given > the previous two)? Error in "[<-"(`*tmp*`, test, value = rep(yes, length =
2017 Dec 13
4
difference between ifelse and if...else?
Hi there, I don't know why the following codes are return different results. > ifelse(3 > 2, 1:3, length(1:3)) [1] 1 > if (3 > 2) 1:3 else length(1:3) [1] 1 2 3 Any hints? Best, Jinsong
2012 May 10
4
additional axis, different scale
Dear list, I am looking for a possibility to present results in a more graphical way by adding an axis. But I have trouble relating my data to the added axis. Imagine the following example: a <- c(10, 20, 30, 40) b <- c(50, 250, 500, 600) ba <- b/a par(las=1, mar=c(5,5,.5,5)) plot(a,b, type="b", pch=22, cex=2, col=4, lwd=2, ylim=c(0,650), xlim=c(0,45)) axis(4,
2018 Jan 08
0
Replace NAs in split lists
Yes, you are right if the IDs are always sequentially-adjacent and the first non-NA value appears in the first record for each ID. -- Sent from my phone. Please excuse my brevity. On January 8, 2018 2:29:40 AM PST, PIKAL Petr <petr.pikal at precheza.cz> wrote: >Hi > >With the example, na.locf seems to be the easiest way. >> library(zoo) > >> na.locf(df1) > ID