Replace query

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace query

burgundy
Hi,

I'm trying to replace all values equal to 1 in one file (a) with the value in the corresponding column in a separate file (b). Example below.
Any help (and brief notes if poss) much appreciated. Thanks!!

file a:
0,0,1,1,0
1,0,0,0,1
0,0,0,0,0
1,0,1,1,0

file b:
3,4,6,8,11

output request:
0,0,6,8,0
3,0,0,0,11
0,0,0,0,0
3,0,6,8,0


Reply | Threaded
Open this post in threaded view
|

Re: Replace query

Tal Galili
The following code might work:

a[a==1] <- b[a==1]

But it depends on what a and b are exactly (vector, matrix, list,
data.frame).


Tal


----------------Contact
Details:-------------------------------------------------------
Contact me: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Sun, May 2, 2010 at 2:01 PM, burgundy <[hidden email]> wrote:

>
> Hi,
>
> I'm trying to replace all values equal to 1 in one file (a) with the value
> in the corresponding column in a separate file (b). Example below.
> Any help (and brief notes if poss) much appreciated. Thanks!!
>
> file a:
> 0,0,1,1,0
> 1,0,0,0,1
> 0,0,0,0,0
> 1,0,1,1,0
>
> file b:
> 3,4,6,8,11
>
> output request:
> 0,0,6,8,0
> 3,0,0,0,11
> 0,0,0,0,0
> 3,0,6,8,0
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Replace-query-tp2122706p2122706.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Replace query

David Winsemius
In reply to this post by burgundy

On May 2, 2010, at 7:01 AM, burgundy wrote:

>
> Hi,
>
> I'm trying to replace all values equal to 1 in one file (a) with the  
> value
> in the corresponding column in a separate file (b). Example below.
> Any help (and brief notes if poss) much appreciated. Thanks!!
>
> file a:
> 0,0,1,1,0
> 1,0,0,0,1
> 0,0,0,0,0
> 1,0,1,1,0

You need to read the introductory material on reading files, and then  
you need to read the Posting Guide about how to present copy-pastable  
versions of r objects. Also look at:

?dput

>
> file b:
> 3,4,6,8,11

Once that is done:

t( t(a)*b)


>
> output request:
> 0,0,6,8,0
> 3,0,0,0,11
> 0,0,0,0,0
> 3,0,6,8,0
>
--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.