Reverse the scoring of some Columns of a Data Set

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

Reverse the scoring of some Columns of a Data Set

AbouEl-Makarim Aboueissa-3
Dear All: goods morning

Is there is a way to reverse the scoring of the first three columns x1, x2,
and x3 and keep the original scores for the fourth column x4.


*Here is an example of the data set:*

x1 x2 x3 x4
2  5   4   4
1  1   1   6
1  2   1   6
2  3   2   4
1  2   1   6
1  3   1   6
2  2   2   5
2  1   1   6
2  2   4   5
5  5   2   1

I am expecting the output to be:
x1 x2 x3 x4
5  5   2   4
2  2   4   6
2  1   1   6
2  2   2   4
1  3   1   6
1  2   1   6
2  3   2   5
1  2   1   6
1  1   1   5
2  5   4   1



thank you very much for your help and support
abou
______________________
AbouEl-Makarim Aboueissa, PhD
Department of Mathematics and Statistics
University of Southern Maine

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Reverse the scoring of some Columns of a Data Set

Jeff Newmiller
Perhaps

dta <- cbind( dta[ rev( seq.int( nrow( dta ) ) ), 1:3 ], dta[ , 4, drop=FALSE ] )

?
--
Sent from my phone. Please excuse my brevity.

On March 8, 2017 6:14:25 AM PST, AbouEl-Makarim Aboueissa <[hidden email]> wrote:

>Dear All: goods morning
>
>Is there is a way to reverse the scoring of the first three columns x1,
>x2,
>and x3 and keep the original scores for the fourth column x4.
>
>
>*Here is an example of the data set:*
>
>x1 x2 x3 x4
>2  5   4   4
>1  1   1   6
>1  2   1   6
>2  3   2   4
>1  2   1   6
>1  3   1   6
>2  2   2   5
>2  1   1   6
>2  2   4   5
>5  5   2   1
>
>I am expecting the output to be:
>x1 x2 x3 x4
>5  5   2   4
>2  2   4   6
>2  1   1   6
>2  2   2   4
>1  3   1   6
>1  2   1   6
>2  3   2   5
>1  2   1   6
>1  1   1   5
>2  5   4   1
>
>
>
>thank you very much for your help and support
>abou
>______________________
>AbouEl-Makarim Aboueissa, PhD
>Department of Mathematics and Statistics
>University of Southern Maine
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Reverse the scoring of some Columns of a Data Set

Daniel Nordlund-3
In reply to this post by AbouEl-Makarim Aboueissa-3
On 3/8/2017 6:14 AM, AbouEl-Makarim Aboueissa wrote:

> Dear All: goods morning
>
> Is there is a way to reverse the scoring of the first three columns x1, x2,
> and x3 and keep the original scores for the fourth column x4.
>
>
> *Here is an example of the data set:*
>
> x1 x2 x3 x4
> 2  5   4   4
> 1  1   1   6
> 1  2   1   6
> 2  3   2   4
> 1  2   1   6
> 1  3   1   6
> 2  2   2   5
> 2  1   1   6
> 2  2   4   5
> 5  5   2   1
>
> I am expecting the output to be:
> x1 x2 x3 x4
> 5  5   2   4
> 2  2   4   6
> 2  1   1   6
> 2  2   2   4
> 1  3   1   6
> 1  2   1   6
> 2  3   2   5
> 1  2   1   6
> 1  1   1   5
> 2  5   4   1
>
>
>
> thank you very much for your help and support
> abou
> ______________________
> AbouEl-Makarim Aboueissa, PhD
> Department of Mathematics and Statistics
> University of Southern Maine
>

  If your data is in a data frame called df, you could do something like
this:

df[,1:3] <- apply(df[,1:3], 2, function(x) x[length(x):1])


Hope this helps,

Dan

--
Daniel Nordlund
Port Townsend, WA  USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Reverse the scoring of some Columns of a Data Set

Nordlund, Dan (DSHS/RDA)
Another alternative (which didn't work last night when I was tired and obviously doing something wrong) is to use the built-in function, rev():

df[,1:3] <- apply(df[,1:3], 2, rev)


Dan

Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services

> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Daniel
> Nordlund
> Sent: Wednesday, March 08, 2017 11:35 PM
> To: AbouEl-Makarim Aboueissa; [hidden email]
> Subject: Re: [R] Reverse the scoring of some Columns of a Data Set
>
> On 3/8/2017 6:14 AM, AbouEl-Makarim Aboueissa wrote:
> > Dear All: goods morning
> >
> > Is there is a way to reverse the scoring of the first three columns
> x1, x2,
> > and x3 and keep the original scores for the fourth column x4.
> >
> >
> > *Here is an example of the data set:*
> >
> > x1 x2 x3 x4
> > 2  5   4   4
> > 1  1   1   6
> > 1  2   1   6
> > 2  3   2   4
> > 1  2   1   6
> > 1  3   1   6
> > 2  2   2   5
> > 2  1   1   6
> > 2  2   4   5
> > 5  5   2   1
> >
> > I am expecting the output to be:
> > x1 x2 x3 x4
> > 5  5   2   4
> > 2  2   4   6
> > 2  1   1   6
> > 2  2   2   4
> > 1  3   1   6
> > 1  2   1   6
> > 2  3   2   5
> > 1  2   1   6
> > 1  1   1   5
> > 2  5   4   1
> >
> >
> >
> > thank you very much for your help and support
> > abou
> > ______________________
> > AbouEl-Makarim Aboueissa, PhD
> > Department of Mathematics and Statistics
> > University of Southern Maine
> >
>
>   If your data is in a data frame called df, you could do something like
> this:
>
> df[,1:3] <- apply(df[,1:3], 2, function(x) x[length(x):1])
>
>
> Hope this helps,
>
> Dan
>
> --
> Daniel Nordlund
> Port Townsend, WA  USA
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.