update cells

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

update cells

Daniel-6
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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: update cells

Jeffrey Ryan
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 <[hidden email]>
Date: Thu, 17 Jun 2010 08:34:16
To: <[hidden email]>
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]]

_______________________________________________
[hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: update cells

Konrad Hoppe
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: [hidden email]
[mailto:[hidden email]] Im Auftrag von
[hidden email]
Gesendet: Donnerstag, 17. Juni 2010 15:09
An: Daniel; [hidden email];
[hidden email]
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 <[hidden email]>
Date: Thu, 17 Jun 2010 08:34:16
To: <[hidden email]>
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]]

_______________________________________________
[hidden email] 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.

_______________________________________________
[hidden email] 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]]


_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: update cells

Patrick Burns-2
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: [hidden email]
> [mailto:[hidden email]] Im Auftrag von
> [hidden email]
> Gesendet: Donnerstag, 17. Juni 2010 15:09
> An: Daniel; [hidden email];
> [hidden email]
> 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<[hidden email]>
> Date: Thu, 17 Jun 2010 08:34:16
> To:<[hidden email]>
> 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]]
>
> _______________________________________________
> [hidden email] 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.
>
> _______________________________________________
> [hidden email] 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]]
>
>
>
>
> _______________________________________________
> [hidden email] 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
[hidden email]
http://www.burns-stat.com

_______________________________________________
[hidden email] 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.