Re: Substituting numerical values using `apply'

View: New views
5 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: Substituting numerical values using `apply'

by Jorge Ivan Velez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Olivella,
Assuming that x is your data, here is one possible way to do what you want:

t(
   apply(x, 1, function(x){
                       index <- x %in% c(1,2)
                       x[index] <- cls[index]
                       x[!index] <- NA
                       x
                       }
           )
  )

How big is the data you are dealing with?

HTH,

Jorge


On Wed, Jul 8, 2009 at 4:25 PM, Olivella <olivella@...> wrote:

>
> Hello,
>
> I wish to perform a substitution of certain numerical values in a data
> matrix with the corresponding column name. For instance, if I have a data
> matrix
> V1  V2  V3
> 2    0    1
> 0    1    2
> 1    5    0
> 5    0    0
>
> I want to substitute the `1' and the `2' for the corresponding column name,
> and make everything else `NA' like this
> V1    V2    V3
> V1    NA    V3
> NA    V2    V3
> V1    NA    NA
> NA    NA    NA
>
> I have done this using an explicit `for' loop, but it takes a really long
> time to finish. Is there any way I can do this using `apply' or some form
> of
> implicit looping?
>
> Thank you for your help,
>
> SO
> --
> View this message in context:
> http://www.nabble.com/Substituting-numerical-values-using-%60apply%27-tp24398687p24398687.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@... 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]]

______________________________________________
R-help@... 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.

Re: Substituting numerical values using `apply'

by Jorge Ivan Velez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Arggghhhh!  Forgot to add "cls" before the code.  Here is the (hopefully)
complete version:
# column names
cls <- colnames(x)

# Transformation
res <- t(
          apply(x, 1, function(x){
                      index <- x %in% c(1,2)
                      x[index] <- cls[index]
                      x[!index] <- NA
                       x
                                }
               )
           )
res

HTH,

Jorge



On Wed, Jul 8, 2009 at 7:15 PM, Jorge Ivan Velez
<jorgeivanvelez@...>wrote:

> Dear Olivella,
> Assuming that x is your data, here is one possible way to do what you want:
>
> t(
>    apply(x, 1, function(x){
>                        index <- x %in% c(1,2)
>                        x[index] <- cls[index]
>                        x[!index] <- NA
>                        x
>                        }
>            )
>   )
>
> How big is the data you are dealing with?
>
> HTH,
>
> Jorge
>
>
> On Wed, Jul 8, 2009 at 4:25 PM, Olivella <olivella@...> wrote:
>
>>
>> Hello,
>>
>> I wish to perform a substitution of certain numerical values in a data
>> matrix with the corresponding column name. For instance, if I have a data
>> matrix
>> V1  V2  V3
>> 2    0    1
>> 0    1    2
>> 1    5    0
>> 5    0    0
>>
>> I want to substitute the `1' and the `2' for the corresponding column
>> name,
>> and make everything else `NA' like this
>> V1    V2    V3
>> V1    NA    V3
>> NA    V2    V3
>> V1    NA    NA
>> NA    NA    NA
>>
>> I have done this using an explicit `for' loop, but it takes a really long
>> time to finish. Is there any way I can do this using `apply' or some form
>> of
>> implicit looping?
>>
>> Thank you for your help,
>>
>> SO
>> --
>> View this message in context:
>> http://www.nabble.com/Substituting-numerical-values-using-%60apply%27-tp24398687p24398687.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help@... 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]]

______________________________________________
R-help@... 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.

Parent Message unknown Re: Substituting numerical values using `apply'

by Henrique Dallazuanna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try this:

sapply(names(DF), function(n)ifelse(DF[,n] %in% c(1, 2), n, NA))

Where DF is your data.frame

On Wed, Jul 8, 2009 at 5:25 PM, Olivella <olivella@...> wrote:

>
> Hello,
>
> I wish to perform a substitution of certain numerical values in a data
> matrix with the corresponding column name. For instance, if I have a data
> matrix
> V1  V2  V3
> 2    0    1
> 0    1    2
> 1    5    0
> 5    0    0
>
> I want to substitute the `1' and the `2' for the corresponding column name,
> and make everything else `NA' like this
> V1    V2    V3
> V1    NA    V3
> NA    V2    V3
> V1    NA    NA
> NA    NA    NA
>
> I have done this using an explicit `for' loop, but it takes a really long
> time to finish. Is there any way I can do this using `apply' or some form
> of
> implicit looping?
>
> Thank you for your help,
>
> SO
> --
> View this message in context:
> http://www.nabble.com/Substituting-numerical-values-using-%60apply%27-tp24398687p24398687.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@... 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
R-help@... 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.

Re: Substituting numerical values using `apply'

by Olivella :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you all for your help.

SO.

Henrique Dallazuanna wrote:

> Try this:
>
> sapply(names(DF), function(n)ifelse(DF[,n] %in% c(1, 2), n, NA))
>
> Where DF is your data.frame
>
> On Wed, Jul 8, 2009 at 5:25 PM, Olivella <olivella@...
> <mailto:olivella@...>> wrote:
>
>
>     Hello,
>
>     I wish to perform a substitution of certain numerical values in a data
>     matrix with the corresponding column name. For instance, if I have
>     a data
>     matrix
>     V1  V2  V3
>     2    0    1
>     0    1    2
>     1    5    0
>     5    0    0
>
>     I want to substitute the `1' and the `2' for the corresponding
>     column name,
>     and make everything else `NA' like this
>     V1    V2    V3
>     V1    NA    V3
>     NA    V2    V3
>     V1    NA    NA
>     NA    NA    NA
>
>     I have done this using an explicit `for' loop, but it takes a
>     really long
>     time to finish. Is there any way I can do this using `apply' or
>     some form of
>     implicit looping?
>
>     Thank you for your help,
>
>     SO
>     --
>     View this message in context:
>     http://www.nabble.com/Substituting-numerical-values-using-%60apply%27-tp24398687p24398687.html
>     Sent from the R help mailing list archive at Nabble.com.
>
>     ______________________________________________
>     R-help@... <mailto:R-help@...> 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.
>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
--
-----------------
Santiago Olivella
Ph.D. Program in Political Science
Washington University in St. Louis
solivella.wustl.edu


        [[alternative HTML version deleted]]


______________________________________________
R-help@... 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.

Parent Message unknown Re: Substituting numerical values using `apply'

by Moshe Olshansky-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Let M be your matrix.

Do the following:

B <- t(matrix(colnames(a),nrow=ncol(M),ncol=nrow(M)))
B[M==0] <- NA

--- On Thu, 9/7/09, Olivella <olivella@...> wrote:

> From: Olivella <olivella@...>
> Subject: [R]  Substituting numerical values using `apply'
> To: r-help@...
> Received: Thursday, 9 July, 2009, 6:25 AM
>
> Hello,
>
> I wish to perform a substitution of certain numerical
> values in a data
> matrix with the corresponding column name. For instance, if
> I have a data
> matrix
> V1  V2  V3
> 2    0    1
> 0    1    2
> 1    5    0
> 5    0    0
>
> I want to substitute the `1' and the `2' for the
> corresponding column name,
> and make everything else `NA' like this
> V1    V2    V3
> V1    NA    V3
> NA    V2    V3
> V1    NA    NA
> NA    NA    NA
>
> I have done this using an explicit `for' loop, but it takes
> a really long
> time to finish. Is there any way I can do this using
> `apply' or some form of
> implicit looping?
>
> Thank you for your help,
>
> SO
> --
> View this message in context: http://www.nabble.com/Substituting-numerical-values-using-%60apply%27-tp24398687p24398687.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@...
> 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.
>

______________________________________________
R-help@... 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.