Re: [R] replace character by numeric value

2023-09-29 Thread Ebert,Timothy Aaron
ifelse(side == 'SELL', -1, NA))) is an option that would take care of other options. -Original Message- From: R-help On Behalf Of Ben Bolker Sent: Friday, September 29, 2023 10:00 AM To: r-help@r-project.org Subject: Re: [R] replace character by numeric value [External Email]

Re: [R] replace character by numeric value

2023-09-29 Thread Ben Bolker
--Original Message- From: R-help On Behalf Of Enrico Schumann Sent: Thursday, September 28, 2023 3:13 AM To: arnaud gaboury Cc: r-help Subject: Re: [R] replace character by numeric value [External Email] On Wed, 27 Sep 2023, arnaud gaboury writes: I have two data.frames: mydf1 <- stru

Re: [R] replace character by numeric value

2023-09-29 Thread Ebert,Timothy Aaron
Does this work? mynewdf$side <- as.numeric(mynewdf$side) This code would be the next line after your mutate. TIm -Original Message- From: R-help On Behalf Of Enrico Schumann Sent: Thursday, September 28, 2023 3:13 AM To: arnaud gaboury Cc: r-help Subject: Re: [R] replace character

Re: [R] replace character by numeric value

2023-09-28 Thread arnaud gaboury
On Thu, Sep 28, 2023 at 8:18 AM Ivan Calandra wrote: > > Dear Arnaud, > > I don't quite unterstand why you have imbricated ifelse() statements. Do > you have more that BUY (1) and SELL (-1)? If not, why not simply: > mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1, -1)) Yes it w

Re: [R] replace character by numeric value

2023-09-28 Thread Enrico Schumann
On Wed, 27 Sep 2023, arnaud gaboury writes: > I have two data.frames: > > mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty = > 1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "", class > = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class = c("data.table",

Re: [R] replace character by numeric value

2023-09-27 Thread Ivan Calandra
Dear Arnaud, I don't quite unterstand why you have imbricated ifelse() statements. Do you have more that BUY (1) and SELL (-1)? If not, why not simply: mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1, -1)) That would solve the problem. I'm not quite sure exactly what happens,

[R] replace character by numeric value

2023-09-27 Thread arnaud gaboury
I have two data.frames: mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty = 1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "", class = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class = c("data.table", "data.frame")) mydf2 <- structure(list(symbol = c("ET

Re: [R] Replace values based on neither condition

2022-10-28 Thread Luigi Marongiu
Perfect, thank you! On Fri, Oct 28, 2022 at 11:53 AM Rui Barradas wrote: > > Às 10:43 de 28/10/2022, Luigi Marongiu escreveu: > > Hello, > > I have a data frame with a string column. All data that are neither > > "POS" nor "NEG" should've replaced by an NA. How can I implement that > > (even with

Re: [R] Replace values based on neither condition

2022-10-28 Thread Rui Barradas
Às 10:43 de 28/10/2022, Luigi Marongiu escreveu: Hello, I have a data frame with a string column. All data that are neither "POS" nor "NEG" should've replaced by an NA. How can I implement that (even with extra libraries)? My attempts actually wipe out POS and NEG... Thank you ``` df = data.fram

[R] Replace values based on neither condition

2022-10-28 Thread Luigi Marongiu
Hello, I have a data frame with a string column. All data that are neither "POS" nor "NEG" should've replaced by an NA. How can I implement that (even with extra libraries)? My attempts actually wipe out POS and NEG... Thank you ``` df = data.frame(a = 1:5, b = c("", "31.35", "POS", "20.61"

Re: [R] Replace multiple subexpressions at once?

2022-10-25 Thread Bill Dunlap
There probably is a package with such a function, but you can do with one call to sub() if you parenthesize all the subexpressions in the regular expression and use \\1, etc., in the replacement for those parts you want to keep. E.g., > s <- "" > want <- "" > new_regexp <- "(.*<[^>]+ data-pos=\"

[R] Replace multiple subexpressions at once?

2022-10-25 Thread Duncan Murdoch
An R regular expression pattern can contain parenthesized subexpressions, and those can be used later in the same pattern, or in the replacement in sub() and gsub(). What I'd like to do is match several subexpressions and replace all of them. For example, I might have a string like s <-

Re: [R] replace NA into - for specific column

2021-06-03 Thread konstantinos christodoulou
Hi Kai, You can also try this: proband_crc2 %>% mutate(MMR = replace(MMR, MMR =="NA", "-")) Thanks, Kostas On Wed, Jun 2, 2021 at 11:59 AM Jim Lemon wrote: > Hi Kai, > Maybe this will help: > > proband_crc2<-data.frame(MMR=rep(c(NA,"+"),3)) > proband_crc2 > proband_crc2$MMR[is.na(proband_cr

Re: [R] replace NA into - for specific column

2021-06-02 Thread Kai Yang via R-help
Hello Jim,Your example works well.Thank you for your help,Kai On Tuesday, June 1, 2021, 04:59:09 PM PDT, Kai Yang via R-help wrote: Hi List, I have a column MMR in a data frame proband_crc2. The column contents missing value and + (means positive).  I need to replace all missing value

Re: [R] replace NA into - for specific column

2021-06-02 Thread Jim Lemon
Hi Kai, Maybe this will help: proband_crc2<-data.frame(MMR=rep(c(NA,"+"),3)) proband_crc2 proband_crc2$MMR[is.na(proband_crc2$MMR)]<-"-" proband_crc2 Jim On Wed, Jun 2, 2021 at 9:59 AM Kai Yang via R-help wrote: > > Hi List, > I have a column MMR in a data frame proband_crc2. The column content

Re: [R] replace NA into - for specific column

2021-06-01 Thread Jeff Newmiller
A unary negative sign with no number is not a number, so it has to be character. If you are done with computations you can format your numbers as character data and set the NA to "-", but such a conversion will prevent you from performing computations so it is only useful for creating report tab

[R] replace NA into - for specific column

2021-06-01 Thread Kai Yang via R-help
Hi List, I have a column MMR in a data frame proband_crc2. The column contents missing value and + (means positive).  I need to replace all missing value into - (means negative) I did try with difference ways.  Below are my testing, but not any one works. Can someone help me? Thanks, Kai proband_

Re: [R] replace zero in a matrix using random numbers

2021-01-08 Thread Yuan Chun Ding
-project.org Subject: Re: [R] replace zero in a matrix using random numbers ?rnorm tells you that n, the first argument, is the number of observations/random numbers you wish to generate. You asked for 1. So you need to ask for the number of 0's, something like: > a <- matrix(rep(0:1,

Re: [R] replace zero in a matrix using random numbers

2021-01-08 Thread Bert Gunter
?rnorm tells you that n, the first argument, is the number of observations/random numbers you wish to generate. You asked for 1. So you need to ask for the number of 0's, something like: > a <- matrix(rep(0:1, 3), nrow =3) > a [,1] [,2] [1,]01 [2,]10 [3,]01 > a[!a] <-

Re: [R] replace zero in a matrix using random numbers

2021-01-08 Thread Rui Barradas
Hello, Try to get the number of zero values before: i <- er.miRCounts == 0 n <- sum(i) er.miRCounts[i] <- rnorm(n,mean=1, sd=0.1) Also, there is no need for a end-of-instruction ';'. The semi-colon is the instruction separator, so if you put it at the end you will be separating the instructio

[R] replace zero in a matrix using random numbers

2021-01-08 Thread Yuan Chun Ding
Hi R users, I am analyzing miRNA sequence data for a special network analysis, I need to replace zero values in a matrix as random numbers with mean of 1 and standard deviation of 0.1. er.miRCounts[er.miRCounts==0] <- rnorm(1,mean=1, sd=0.1); this code made all zero values as 1.13, not random

Re: [R] Replace double slashes with single backslash

2020-12-28 Thread Anbu A
Thank you, Jim. That clarifies. I am trying to pass this path in a loop and read the files associated with the path. Yes the length is 26 where double quotes are counted as single quotes. Let me try to read the files using the collected path. Really appreciate it. Thanks, Anbu On Mon, Dec 28, 2

Re: [R] Replace double slashes with single backslash

2020-12-28 Thread jim holtman
Why do you want to replace '\\' with '\' in the file names? They are actually single '\' in the character string, but are printing out as '\\'. see example below: > x <- 'a\\b' > x [1] "a\\b" > nchar(x) [1] 3 Jim Holtman *Data Munger Guru* *What is the problem that you are

Re: [R] Replace double slashes with single backslash

2020-12-28 Thread Bert Gunter
"\" is an escape in R. See ?Quotes for details. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Dec 28, 2020 at 12:56 PM Anbu A wrote: > Hi All, > I am a

[R] Replace double slashes with single backslash

2020-12-28 Thread Anbu A
Hi All, I am able to replace "r" with "x" for the word "Users" just for a test run. *Code: newlist %>% mutate(.,new_col=str_replace(fpath,"r","x")) *- this works fine But when I try to replace "\\" with "\". *newlist %>% mutate(.,new_col=str_replace(fpath,"\\","\")) *, I get a prompt ">" to comple

Re: [R] Replace NAs in split lists

2018-01-08 Thread PIKAL Petr
> From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] > Sent: Monday, January 8, 2018 4:45 PM > To: Eric Berger > Cc: PIKAL Petr ; r-help@r-project.org; Ek Esawi > > Subject: Re: [R] Replace NAs in split lists > > "Enforce" is overstating it... results will differ

Re: [R] Replace NAs in split lists

2018-01-08 Thread Ek Esawi
Thank you all. Now everything works. Happy 2018 and beyond EK On Sun, Jan 7, 2018 at 10:13 PM, Ek Esawi wrote: > 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

Re: [R] Replace NAs in split lists

2018-01-08 Thread ruipbarradas
Because you need to separate the instructions with a ; (semi-colon). Hope this helps Rui Barradas Enviado a partir do meu smartphone Samsung Galaxy. Mensagem original De: Ek Esawi Data: 08/01/2018 16:03 (GMT+00:00) Para: Jeff Newmiller , r-help@r-project.org Assunto: Re: [R

Re: [R] Replace NAs in split lists

2018-01-08 Thread William Dunlap via R-help
I don't get exactly that error message, > ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z}) Error: unexpected symbol in "ifelse(is.na(z$Value),z$Value[!is.na (z$Value)][1],z$Value)z" The 'symbol' in "unexpected symbol" refers to a "name" ('z' in this case). The problem is usually at

Re: [R] Replace NAs in split lists

2018-01-08 Thread Ek Esawi
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 wrote: > I don't know. You seem to be posting in HTML so your code is mangled

Re: [R] Replace NAs in split lists

2018-01-08 Thread Jeff Newmiller
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 wrote: >Thank you Jeff.

Re: [R] Replace NAs in split lists

2018-01-08 Thread Ek Esawi
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 O

Re: [R] Replace NAs in split lists

2018-01-08 Thread Jeff Newmiller
>> >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-boun...@r-project.org] On Behalf

Re: [R] Replace NAs in split lists

2018-01-08 Thread Eric Berger
gt; From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff > >> Newmiller > >> Sent: Monday, January 8, 2018 9:13 AM > >> To: r-help@r-project.org; Ek Esawi > >> Subject: Re: [R] Replace NAs in split lists > >> > >> Upon closer examin

Re: [R] Replace NAs in split lists

2018-01-08 Thread Jeff Newmiller
om: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff >> Newmiller >> Sent: Monday, January 8, 2018 9:13 AM >> To: r-help@r-project.org; Ek Esawi >> Subject: Re: [R] Replace NAs in split lists >> >> Upon closer examination I see that you are not using

Re: [R] Replace NAs in split lists

2018-01-08 Thread PIKAL Petr
lp [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff > Newmiller > Sent: Monday, January 8, 2018 9:13 AM > To: r-help@r-project.org; Ek Esawi > Subject: Re: [R] Replace NAs in split lists > > Upon closer examination I see that you are not using the split version of > df1

Re: [R] Replace NAs in split lists

2018-01-08 Thread Jeff Newmiller
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 FALSENA 3 a ac FALSENA 4 b aa TRUE 5 5 b ab FALSE

Re: [R] Replace NAs in split lists

2018-01-07 Thread Jeff Newmiller
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 bre

Re: [R] Replace NAs in split lists

2018-01-07 Thread PIKAL Petr
wi > Sent: Monday, January 8, 2018 4:36 AM > To: r-help@r-project.org > Subject: Re: [R] Replace NAs in split lists > > I just came up with a solution right after i posted the question, but i > figured > there must be a better and shorter one.than my solution sdf1[[1]][1,4]<- >

Re: [R] Replace NAs in split lists

2018-01-07 Thread Ek Esawi
I just came up with a solution right after i posted the question, but i figured there must be a better and shorter one.than my solution sdf1[[1]][1,4]<-lapplyresults[[1]] sdf1[[2]][1,4]<-lapplyresults[[2]] EK On Sun, Jan 7, 2018 at 10:13 PM, Ek Esawi wrote: > Hi all-- > > I stumbled on this prob

[R] Replace NAs in split lists

2018-01-07 Thread Ek Esawi
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 desi

Re: [R] replace

2017-03-14 Thread PIKAL Petr
;Bob" "Bob" [7] "Alex-2002" "Alex-2003" "Alex-2004" Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Marc > Schwartz > Sent: Tuesday, March 14, 2017 12:33 AM > To: Val &

Re: [R] replace

2017-03-13 Thread Marc Schwartz
> On Mar 13, 2017, at 5:53 PM, Val wrote: > > HI all, > > if first name is Alex then I want concatenate the second column to Alex > to produce Alex and the second column value > > DF1 <- read.table(header=TRUE, text='first YR > Alex2001 > Bob 2001 > Cory2001 > Cory2002 > Bob

[R] replace

2017-03-13 Thread Val
HI all, if first name is Alex then I want concatenate the second column to Alex to produce Alex and the second column value DF1 <- read.table(header=TRUE, text='first YR Alex2001 Bob 2001 Cory2001 Cory2002 Bob 2002 Bob 2003 Alex2002 Alex2003 Alex2004') Out

Re: [R] Replace Text but not from within a word

2017-02-28 Thread Marc Schwartz
> On Feb 28, 2017, at 8:36 AM, Jeff Newmiller wrote: > > For tasks like this, you will probably want to make sure to import the data > as character data rather than as a factor. E.g. > > dat <- read.csv( "myfile.csv", header=FALSE, as.is=TRUE ) > > You can check what you have with the str()

Re: [R] Replace Text but not from within a word

2017-02-28 Thread Jeff Newmiller
For tasks like this, you will probably want to make sure to import the data as character data rather than as a factor. E.g. dat <- read.csv( "myfile.csv", header=FALSE, as.is=TRUE ) You can check what you have with the str() function. -- Sent from my phone. Please excuse my brevity. On Februa

Re: [R] Replace Text but not from within a word

2017-02-28 Thread Marc Schwartz
> On Feb 28, 2017, at 3:38 AM, Harshal Athawale > wrote: > > I am new in R. > > I have a file. This file contains name of the companies. > 'data.frame': 494 obs. of 1 variable: > $ V1: Factor w/ 470 levels "3-d engineering corp",..: 293 134 339 359 143 > 399 122 447 398 384 ... > > Problem:

[R] Replace Text but not from within a word

2017-02-28 Thread Harshal Athawale
I am new in R. I have a file. This file contains name of the companies. 'data.frame': 494 obs. of 1 variable: $ V1: Factor w/ 470 levels "3-d engineering corp",..: 293 134 339 359 143 399 122 447 398 384 ... Problem: I would like to remove "CO" (As it is the most frequent word). I would like "C

Re: [R] Replace a dot in a string (".") with a space (" ")

2016-11-19 Thread Rui Barradas
And here is another. sub("\\.", " ", "c.t") Rui Barradas Em 19-11-2016 01:35, Bert Gunter escreveu: Well of course. See ?regexp where it says: "The period . matches any single character." (Always a good idea to read man pages, although this *is* a complex one) Setting the fixed argument to

Re: [R] Replace a dot in a string (".") with a space (" ")

2016-11-18 Thread Bert Gunter
Well of course. See ?regexp where it says: "The period . matches any single character. " (Always a good idea to read man pages, although this *is* a complex one) Setting the fixed argument to TRUE is one way to get what you want (there are others). > sub(".", " ", "c.t",fixed=TRUE) [1] "c t"

Re: [R] Replace a dot in a string (".") with a space (" ")

2016-11-18 Thread Omar André Gonzáles Díaz
Hi, your were close. This is the solution: sub("[.]", " ", "c.t") You need to scape the point, because point has a especial meaning in regular expressions. Read more on regex... 2016-11-18 19:13 GMT-05:00 John : > Hi, > >Is there any function that replaces a dot with a space? I expect "c t

[R] Replace a dot in a string (".") with a space (" ")

2016-11-18 Thread John
Hi, Is there any function that replaces a dot with a space? I expect "c t" from the output of the second call of function sub, but it did not do so. > sub("a", "b", "cat") [1] "cbt" > sub(".", " ", "c.t") [1] " .t" Thanks! [[alternative HTML version deleted]] ___

Re: [R] replace items in vector with character based on logical operator

2016-11-12 Thread William Dunlap via R-help
Your first replacement in kx changed it to a character vector and comparisons of character strings given different results than comparisons of numbers > kx <- 1:10 > qt <- quantile(kx) > kx[kx <= qt[2]] <- "DN" > kx [1] "DN" "DN" "DN" "4" "5" "6" "7" "8" "9" "10" > "10" < "6" [1] TRUE > > #

Re: [R] replace items in vector with character based on logical operator

2016-11-12 Thread Jeff Newmiller
Your goal is fundamentally flawed, since all elements of a vector must be if the same type. Create another vector to hold your character information. kc <- rep( 'NC', length( kx ) ) kc[ kx <= qt[[2]] ] <- 'DN' kc[ kx >=qt [[4]] ] <- 'UP' -- Sent from my phone. Please excuse my brevity. On N

[R] replace items in vector with character based on logical operator

2016-11-12 Thread Adrian Johnson
Hi group, I have a vector of numeric items and I want to replace based on logical operator (> or <) with 'up', 'down' or 'nochange' The way I am doing works sometimes and does not work sometime. I dont understand the problem. In this example, that works for condition DN first, gets over-written

Re: [R] Replace any values in a data frame based on another data frame

2016-07-30 Thread Jeff Newmiller
Your use of HTML email corrupted your example slightly, but I was able to fix it. Please follow the Posting Guide and set your emails to Plain Text mode when posting to this mailing list in the future. Here is one way: # you have to be careful about mucking with factors # convert columns to fa

Re: [R] Replace any values in a data frame based on another data frame

2016-07-30 Thread Bert Gunter
Marine: Thanks for the reproducible example. I would not have fooled with this otherwise! 1. First note that your specification that you wish to replace only those values in col3 and col4 of df1 that don't match with those of df2 is irrelevant: if you replace those that do match you don't change

[R] Replace any values in a data frame based on another data frame

2016-07-30 Thread Marine Regis
Hello, I have two data frames with different sizes but with the same number of columns. > df1 <- data.frame(col1 = c(1:6), col2 = c(rep("a", 3), rep("b", 3)), col3 = > c(rep("AA", 2), rep("BB", 2), rep("CC", 2)), col4=c(1,8,6,9,7,6)) > df1 col1 col2 col3 col4 11a AA1 22

Re: [R] replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

2016-06-06 Thread Bert Gunter
Sidenote: When you do imputation in this way all your inference (error estimates, goodness of fit, confidence intervals, etc.) will be wrong and misleading, as you are creating "information" from nothing. If this comment is irrelevant, please ignore. Cheers, Bert Bert Gunter "The trouble with h

Re: [R] replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

2016-06-06 Thread Henrique Dallazuanna
Maybe you can use something like this In this way, almost your proportion of 0, 1 and 2 will be maintained m <- matrix(sample(c(NA, 0:2), size = 50*100, replace = TRUE), nrow = 50, ncol = 100) trunc(prop.table(apply(m, 2, table), 2) * colSums(is.na(m)), 0) m[is.na(m)] <- unlist(apply(trunc(prop.t

[R] replace NAs in each column of a matrix with 0 or 1 or 2 with specific proportion

2016-06-06 Thread Lida Zeighami
Hello specialist, I have a matrix in which there are NA,0,1 and 2 in each columns. I wanna replace NAs with special proportion of 0,1 or 2 ! for example in df<- matric(df, nrow=50, ncol=100) If in one column the number of NAs = 10 , # of 0=50 , #of 1=25 and # of 2=15 I want to replace 5 of 10 N

Re: [R] replace text by uniqe number

2016-03-09 Thread Jim Lemon
Hi Ragia, If you have read in your data frame with read.table or similar and not specified stringsAsFactors=FALSE, the two columns will already be factors. However, unless they both contain the same number of unique values, the numbers associated with those levels won't be the same. Probably the ea

Re: [R] replace text in table R?

2016-03-09 Thread David Winsemius
> On Mar 8, 2016, at 1:48 PM, Sarah Goslee wrote: > > Thanks for the complete reproducible example. > > Here's one way to approach the problem; there are many others. > > recodeDat <- function(x, invals, outvals) { > x <- as.character(x) > invals <- as.character(invals) > outvals <- as.charact

Re: [R] replace text by uniqe number

2016-03-09 Thread PIKAL Petr
)) Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ragia . > Sent: Wednesday, March 09, 2016 9:11 AM > To: r-help@r-project.org > Subject: [R] replace text by uniqe number > > Dear group > kindly if i had data fra

[R] replace text by uniqe number

2016-03-09 Thread Ragia .
Dear group kindly if i had data frame of 2 columns that has repeated URLS ..and want to replace those urls with ID's for each one so the url will have unique ID, how  can I do this thanks in advance e.g otf  data pcworld.com open.itworld.com pcworld.com storage.itworld.com salon.com

Re: [R] replace text in table R?

2016-03-08 Thread Sarah Goslee
Thanks for the complete reproducible example. Here's one way to approach the problem; there are many others. recodeDat <- function(x, invals, outvals) { x <- as.character(x) invals <- as.character(invals) outvals <- as.character(outvals) # a loop is the most understandable approach for(i in seq_a

[R] replace text in table R?

2016-03-08 Thread Marna Wagley
Hi R Users, I have been struggling to replace texts in a table by new text. but it seems crazy as of I am doing it manually in R. I have very big files and some of the text has to be replaced by another class based on another file if the name corresponds. I was able to perform following example but

Re: [R] replace NA's with row means for specific columns

2015-11-12 Thread Marco
Excerpts from Zahra via R-help's message of 2015-11-02 17:49:01 -0200: > Hi there, > > I am looking for some help replacing missing values in R with the row mean. > This is survey data and I am trying to impute values for missing variables in > each set of questions separately using the mean of

Re: [R] replace NA's with row means for specific columns

2015-11-03 Thread David L Carlson
David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon Sent: Monday, November 2, 2015 5:33 PM To: Zahra Cc: r-help mailing list Subject

Re: [R] replace NA's with row means for specific columns

2015-11-02 Thread Jim Lemon
Hi again, Small typo in line 5 - should be replace_NAs<-function(x,group_lab=c("A","B","C")) { for(lab in group_lab) { indices<-grep(lab,names(x),fixed=TRUE) na_indices<-is.na(x[indices]) if(any(na_indices)) x[indices][na_indices]<-rowMeans(x[indices],na.rm=TRUE) } return(x) } Jim

Re: [R] replace NA's with row means for specific columns

2015-11-02 Thread Jim Lemon
Hi Zahra, I can't think of an "apply" function that will do this, but: Zdf<-read.table(text="ID A1 A2 A3 B1 B2 B3 C1 C2 C3 C4 b 4 5 NA2 NA 4 5 13 NA c 4 5 1 NA 34 5 13 2

[R] replace NA's with row means for specific columns

2015-11-02 Thread Zahra via R-help
Hi there, I am looking for some help replacing missing values in R with the row mean. This is survey data and I am trying to impute values for missing variables in each set of questions separately using the mean of the scores for the other questions within that set. I have a dataset that look

Re: [R] Replace NaN with value from the same row

2015-10-18 Thread Jonathan Reardon
Ok, i will do, thanks for your help. J > Subject: RE: [R] Replace NaN with value from the same row > From: jdnew...@dcn.davis.ca.us > Date: Sun, 18 Oct 2015 12:55:14 -0700 > To: jonathanrear...@outlook.com > CC: r-help@r-project.org > > You should (re-)read the intro docum

Re: [R] Replace NaN with value from the same row

2015-10-18 Thread Jeff Newmiller
is making a new >column called 'idx', finds the NaN values and inserts the boolean TRUE >in the respective cell. >df[ idx, "mean" ] <- df[ idx, "offset" ] << i am unsure what this >is doing exactly. >Jon > > >> Subject: RE: [R] Rep

Re: [R] Replace NaN with value from the same row

2015-10-18 Thread Jeff Newmiller
AM PDT, Jonathan Reardon wrote: >How do i send an email in plain text format and not HTML? >I tried: >idx <- is.na( df$mean ) >df[ idx, "mean" ] <- df[ idx, "offset" ] >I got the error message: >In is.na(df$mean) : is.na() applied to non-(list or ve

[R] Replace NaN from 1 column with a value from the same row

2015-10-18 Thread Jonathan Reardon
Hi everyone, Ignore my previous post, i realised that the rows and columns i typed into the email were unreadable, sincere apologies for this. A simple question, but i cannot figure this out. I have a data-frame with 4 columns (onset, offset, outcome, mean): df<-data.frame(onset=c(72071,142598,293

Re: [R] Replace NaN with value from the same row

2015-10-18 Thread Jeff Newmiller
Next time send your email using plain text format rather than HTML so we see what you saw. Try idx <- is.na( df$mean ) df[ idx, "mean" ] <- df[ idx, "offset" ] BTW there is a commonly-used function called df, so you might improve clarity by using DF for your temporary data frame name. ---

[R] Replace NaN with value from the same row

2015-10-18 Thread Jonathan Reardon
Hi everyone, A simple question, but i cannot figure this out. I have a data-frame with 4 columns (onset, offset, outcome, mean): onset offset outcome mean8 72071 72503 1 7244615 142598 143030 1NaN30 293729 294161 1 294080 For each 'NaN' in the mean column, i want to r

Re: [R] Replace values in a dataframe

2015-06-17 Thread Grams Robins via R-help
Try this:  dat=structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen","Hazen","Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names =c("1:2","1:3", "1:4", "1:5","1:6"), class = "data.frame") dat=as.data.frame(dat)dat$col2 <- rep(" ", nrow(dat))dat[dat$Color == "<4", ][

Re: [R] Replace values in a dataframe

2015-06-17 Thread Bert Gunter
Is the following what you want: (z is your data frame) > change <-c("2","2.5") > names(change) <- c("<4","<5") (note: this can be automated using regular expressions and will work for lots more values to change. Sarah's ifelse() solution is fine for the example, but becomes too cumbersome (as sh

Re: [R] Replace values in a dataframe

2015-06-17 Thread Sarah Goslee
Hi Shane, On Wed, Jun 17, 2015 at 1:31 PM, Shane Carey wrote: > Hey all, > > I have a dataframe that consists of: > > structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", > "Hazen", > "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names = > c("1:2", > "1:3", "1:4",

[R] Replace values in a dataframe

2015-06-17 Thread Shane Carey
Hey all, I have a dataframe that consists of: structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", "Hazen", "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names = c("1:2", "1:3", "1:4", "1:5","1:6"), class = "data.frame") I need to find the <4 and have a new colum

Re: [R] Replace a column value on condition

2015-03-23 Thread PIKAL Petr
elp [mailto:r-help-boun...@r-project.org] On Behalf Of Kuma > Raj > Sent: Monday, March 23, 2015 1:59 PM > To: r-help@r-project.org > Subject: [R] Replace a column value on condition > > I want to replace column c3 with values from column c2 whenever values > of column Id are 2.

Re: [R] Replace a column value on condition

2015-03-23 Thread Berend Hasselman
> On 23-03-2015, at 13:58, Kuma Raj wrote: > > I want to replace column c3 with values from column c2 whenever values > of column Id are 2. In stata I could use replace c3 = c2 if id ==2. > How could I do that in R? > k <- which(df4$id==2) df4[k,"c3"] <- df4[k,"c2”] Berend > Thanks > > >

[R] Replace a column value on condition

2015-03-23 Thread Kuma Raj
I want to replace column c3 with values from column c2 whenever values of column Id are 2. In stata I could use replace c3 = c2 if id ==2. How could I do that in R? Thanks Sample data found below: > dput(df4) structure(list(c2 = c(42L, 42L, 47L, 47L, 55L, 55L, 36L, 36L, 61L, 61L), c3 = c(68L, 5

Re: [R] Why does R replace all row values with NAs

2015-03-01 Thread peter dalgaard
> On 27 Feb 2015, at 16:02 , Duncan Murdoch wrote: > > Yes. Indexing with a logical NA is probably a mistake, and this is one > way to signal it without actually triggering a warning or error. There are cases where it isn't (usually) a mistake, e.g. pch=c(25,24)[sex], where it is quite crucia

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Dimitri Liakhovitski
Thank you very much guys! On Fri, Feb 27, 2015 at 11:04 AM, William Dunlap wrote: > You could define functions like >is.true <- function(x) !is.na(x) & x >is.false <- function(x) !is.na(x) & !x > and use them in your selections. E.g., > > x <- data.frame(a=1:10,b=2:11,c=c(1,NA,3,NA,5,N

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread William Dunlap
You could define functions like is.true <- function(x) !is.na(x) & x is.false <- function(x) !is.na(x) & !x and use them in your selections. E.g., > x <- data.frame(a=1:10,b=2:11,c=c(1,NA,3,NA,5,NA,7,NA,NA,10)) > x[is.true(x$c >= 6), ] a b c 7 7 8 7 10 10 11 10 Bill Dun

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Duncan Murdoch
On 27/02/2015 10:27 AM, Dimitri Liakhovitski wrote: > Thank you very much, Duncan. > All this being said: > > What would you say is the most elegant and most safe way to solve such > a seemingly simple task? If you have NA values, test for them explicitly, e.g. your original x[(x$c<6) | is.na(x$

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Dimitri Liakhovitski
Thank you very much, Duncan. All this being said: What would you say is the most elegant and most safe way to solve such a seemingly simple task? Thank you! On Fri, Feb 27, 2015 at 10:02 AM, Duncan Murdoch wrote: > On 27/02/2015 9:49 AM, Dimitri Liakhovitski wrote: >> So, Duncan, do I understan

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Duncan Murdoch
On 27/02/2015 9:49 AM, Dimitri Liakhovitski wrote: > So, Duncan, do I understand you correctly: > > When I use x$x<6, R doesn't know if it's TRUE or FALSE, so it returns > a logical value of NA. Yes, when x$x is NA. (Though I think you meant x$c.) > When this logical value is applied to a row,

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Dimitri Liakhovitski
So, Duncan, do I understand you correctly: When I use x$x<6, R doesn't know if it's TRUE or FALSE, so it returns a logical value of NA. When this logical value is applied to a row, the R says: hell, I don't know if I should keep it or not, so, just in case, I am going to keep it, but I'll replace

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread Duncan Murdoch
On 27/02/2015 9:04 AM, Dimitri Liakhovitski wrote: > I know how to get the output I need, but I would benefit from an > explanation why R behaves the way it does. > > # I have a data frame x: > x = data.frame(a=1:10,b=2:11,c=c(1,NA,3,NA,5,NA,7,NA,NA,10)) > x > # I want to toss rows in x that conta

[R] Why does R replace all row values with NAs

2015-02-27 Thread Dimitri Liakhovitski
I know how to get the output I need, but I would benefit from an explanation why R behaves the way it does. # I have a data frame x: x = data.frame(a=1:10,b=2:11,c=c(1,NA,3,NA,5,NA,7,NA,NA,10)) x # I want to toss rows in x that contain values >=6. But I don't want to toss my NAs there. subset(x,c

Re: [R] Replace the value with 1 and 0

2015-02-26 Thread Göran Broström
On 2015-02-26 00:33, JS Huang wrote: Hi, Here is an implementation. More data are added. An extra column hasRain is added instead of replacing column Amount. rain Year Month Day Amount 1 1950 1 10.0 2 1950 1 2 35.5 3 1950 1 3 17.8 4 1950 1 4 24

Re: [R] Replace the value with 1 and 0

2015-02-25 Thread JS Huang
Hi, Here is an implementation. More data are added. An extra column hasRain is added instead of replacing column Amount. > rain Year Month Day Amount 1 1950 1 10.0 2 1950 1 2 35.5 3 1950 1 3 17.8 4 1950 1 4 24.5 5 1950 1 5 12.3 6 1950 1

Re: [R] Replace the value with 1 and 0

2015-02-25 Thread Clint Bowman
me tidying up. HTH Peter Alspach -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of smart hendsome Sent: Thursday, 26 February 2015 11:54 a.m. To: r-help@r-project.org Subject: [R] Replace the value with 1 and 0 Hi everyone, I have this kind of rainfall da

Re: [R] Replace the value with 1 and 0

2015-02-25 Thread Peter Alspach
;month')], sum) will give your final table, but it will need some tidying up. HTH Peter Alspach -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of smart hendsome Sent: Thursday, 26 February 2015 11:54 a.m. To: r-help@r-project.org Subject: [R] Re

[R] Replace the value with 1 and 0

2015-02-25 Thread smart hendsome
Hi everyone, I have this kind of rainfall dataset:    Year Month Day Amount 1  1950 1   1    0.0 2  1950 1   2   35.5 3  1950 1   3   17.8 4  1950 1   4   24.5 5  1950 1   5   12.3 6  1950 1   6   11.5 7  1950 1   7    5.7 8  1950 1   8   13.2 9  1950 1   9   11.

Re: [R] replace values in one df by values from key pairs in another df

2015-02-13 Thread arnaud gaboury
This is the wrong part of my code. > >> idName=users[users$id %in% ext] > idname > 1: U03AEKWTL agreenmamba > 2: U032FHV3S poisonivy > 3: U03AEKYL4 vairis > Best is to use: idNames <- users[pmatch(ext, users$id, duplicates.ok = T)]. This leave me with an ordered and dupl

  1   2   3   4   5   >