[R-SIG-Finance] update cells

Patrick Burns patrick at burns-stat.com
Fri Jun 18 10:47:54 CEST 2010


As long as there is an answer on the list,
I'll take the liberty of showing what I
think is a better answer.

* The 'for' loop is not very R-ish.

* If the name columns are factors, you are
probably in trouble.

myTable <- data.frame(name_x=c('a', ' ', 'c', NA),
    name_y=LETTERS[1:4], stringsAsFactors=FALSE)

myTable2 <- within(myTable, name_x <-
    ifelse(is.na(name_x) | name_x == " ", name_y, name_x))



On 17/06/2010 23:05, Konrad Hoppe wrote:
> Hi,
>
> yes, it's really not a finance question, but I think it's okay to give an
> answer shortly:
>
> Assuming your table is named myTable, just do the following:
>
> for(i in 1:nrow(myTable)){
> 	if(is.na(myTable$name_x[i]) || (myTable$name_x[i] == " ")){
> 		myTable$name_x[i]<- myTable$name_y[i]
> 	}
> }
>
> This should work. I haven't tested it due to the fact that you haven't
> provided your first tries as a code snippet, but I'm entirely sure this will
> work. But in general I would do it via sql, this will employ the solution
> much faster.
>
> Best regards,
>
> Konrad Hoppe
> www.konrad-hoppe.com
>
>
> -----Ursprüngliche Nachricht-----
> Von: r-sig-finance-bounces at stat.math.ethz.ch
> [mailto:r-sig-finance-bounces at stat.math.ethz.ch] Im Auftrag von
> jeff.a.ryan at gmail.com
> Gesendet: Donnerstag, 17. Juni 2010 15:09
> An: Daniel; r-sig-finance-bounces at stat.math.ethz.ch;
> r-sig-finance at stat.math.ethz.ch
> Betreff: Re: [R-SIG-Finance] update cells
>
> Not a Finance question. Please follow the posting guidelines and redirect to
> R-help
>
> Jeff
> Sent via BlackBerry from T-Mobile
>
> -----Original Message-----
> From: Daniel<dmsilv at gmail.com>
> Date: Thu, 17 Jun 2010 08:34:16
> To:<r-sig-finance at stat.math.ethz.ch>
> Subject: [R-SIG-Finance] update cells
>
> Hello all,I have a table with about 18000 rows, I need to UPDATE the strings
> in the "name.x" column in each row by the strings from "name.y" given if
> "name.x" is NA or " ". How can I do it?
> Also I want to update values in other columns, but I think that will be the
> same job.
> Daniel
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
>
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
>
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>
> 	[[alternative HTML version deleted]]
>
>
>
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

-- 
Patrick Burns
patrick at burns-stat.com
http://www.burns-stat.com



More information about the R-SIG-Finance mailing list