[R] Function, that assigns two vectors to each other

arun smartpink111 at yahoo.com
Thu Jul 25 16:20:41 CEST 2013


HI,
You could try:
my.data<- structure...
pe<-round(pe,0)
peMax<-as.data.frame(sapply(paste0("a",1:4),function(x) {x1<-my.data[,x]; unsplit(lapply(split(x1,x1),function(y) {x2<-row.names(pe)[pe[,x]%in% y]; x3<-x2[which.max(as.numeric(gsub("\\D+","",x2)))];rep(x3,length(y))}),x1)}),stringsAsFactors=FALSE)
 names(peMax)<- paste0("pe",1:4)
 my.dataNew<- cbind(my.data,peMax)
 
  my.dataNew
#   pa  a1  a2  a3  a4  pe1  pe2  pe3  pe4
#1   1  84 113  96  76  12%  89%  24%   0%
#2   2 108 101 108 106  90%  45%  80%  72%
#3   3 113  99 110  94 100%  36%  89%  25%
#4   4  99 108  99 124  78%  61%  49% 100%
#5   5  98 122 118  91  72% 100% 100%  12%
#6   6  88  92 100 103  27%  12%  58%  46%
#7   7  90  90  90 107  45%   2%  12%  78%
#8   8  89 110  89 106  38%  79%   5%  72%
#9   9  95 109 102 113  57%  72%  67%  89%
#10 10  77  95  99  96   0%  23%  49%  34%

If you don't want the "%" attached to the number
 my.dataNew[,6:9]<-lapply(my.dataNew[6:9],function(x) as.numeric(gsub("\\D+","",x)))
 my.dataNew
#   pa  a1  a2  a3  a4 pe1 pe2 pe3 pe4
#1   1  84 113  96  76  12  89  24   0
#2   2 108 101 108 106  90  45  80  72
#3   3 113  99 110  94 100  36  89  25
#4   4  99 108  99 124  78  61  49 100
#5   5  98 122 118  91  72 100 100  12
#6   6  88  92 100 103  27  12  58  46
#7   7  90  90  90 107  45   2  12  78
#8   8  89 110  89 106  38  79   5  72
#9   9  95 109 102 113  57  72  67  89
#10 10  77  95  99  96   0  23  49  34
A.K. 



----- Original Message -----
From: Anne-Marie B. Gallrein <gallrein at psychologie.tu-dresden.de>
To: John Kane <jrkrideau at inbox.com>
Cc: r-help at r-project.org
Sent: Thursday, July 25, 2013 7:28 AM
Subject: Re: [R] Function, that assigns two vectors to each other

Hello guys, I created an example data set:

structure(list(pa = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), a1 = c(84,
108, 113, 99, 98, 88, 90, 89, 95, 77), a2 = c(113, 101, 99, 108,
122, 92, 90, 110, 109, 95), a3 = c(96, 108, 110, 99, 118, 100,
90, 89, 102, 99), a4 = c(76, 106, 94, 124, 91, 103, 107, 106,
113, 96)), .Names = c("pa", "a1", "a2", "a3", "a4"), row.names = c(NA,
-10L), class = "data.frame")

So the data frame contains the numbers of my participants (1 to 10) and the score, they hit on 4 tasks (a1 to a4).

I wrote this function, to use on the data:


pe<-apply(X=my.data[,c("a1","a2","a3","a4")],
          
           MARGIN=2,
          
           FUN=quantile,
          
           probs=seq(0,1,by=.01),
          
           na.rm=TRUE)

round(pe,0)


It computes the percentiles of each task. So when using this function I know, that e.g.
a person who got 77 points on task 1 (a1) has a percentile of 0%.
If a person scores 88 points then he/she got the percentiles 21% to 27%, so 27% got the same amount of points or less.
In comparison in task 4 (a4) a person reaching 77 points has a percentile of 1%.

Now I want to add 4 columns to my.data (pe1 to pe4).

The final data frame my.data shall have 10 rows and 9 columns

These columns (pe1 to pe4) shall show the maximum percentile someone reached according to his points for each task.
So for the person who reached 77 points in a1 the respective pe1 would be 0.
For all the people who reached 88 points in a1 the respective pe1 would be 27.
For all the people who reached 77 points in a4 the respective pe1 would be 1.
The final data frame my.data shall have 10 rows and 9 columns.

So for the first participant (pa=1), the pe's would be a1=84  --> pe=12; a2=113  --> pe=89, a3=96 --> pe=24, a4=76 --> pe=0

I hope, that is clearer than before :)

Thanks a lot,

Anne




Am 24.07.2013 14:47, schrieb John Kane:
> Welcome  to R-help
> it is a bit hard to see exactly what you want without data. Rest of the explanation looks good though it appears you may have sent this in HTML and the list asks for text.  It strips out the html and we lose any html format.
>
> Can I suggest reading these https://github.com/hadley/devtools/wiki/Reproducibility
>  http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
> and then getting back to use with some data.  The best way to provide data , as is described in the above links is to use dput()  (type ?dput for help ) and then just copy and paste the results into the mail.
>
>
>
> John Kane
> Kingston ON Canada
>
>
>> -----Original Message-----
>> From: gallrein at psychologie.tu-dresden.de
>> Sent: Wed, 24 Jul 2013 12:25:35 +0200
>> To: r-help at r-project.org
>> Subject: [R] Function, that assigns two vectors to each other
>>
>> Hey guys,
>>
>> In my data setv ("KD") I have 4 columns
>> ("Punkte.AG1","Punkte.AG2","Punkte.AG3","Punkte.WI") I'm interested in.
>>
>> These columns contain the participants' scores of a specific task.
>>
>> I computed the percentiles of the columns using this code:
>>
>> pe<-apply(X=KD[,c("Punkte.AG1","Punkte.AG2","Punkte.AG3","Punkte.WI")],
>>
>> MARGIN=2,
>>
>> FUN=quantile,
>>
>> probs=seq(0,1,by=.01),
>>
>> na.rm=TRUE)
>>
>> round(pe,0)
>>
>>
>> This is the output (to the 20^th percentile):
>>
>> pe
>>
>> Punkte.AG1 Punkte.AG2 Punkte.AG3 Punkte.WI
>>
>> 0%6319
>>
>> 1%74311
>>
>> 2%86312
>>
>> 3%87412
>>
>> 4%97512
>>
>> 5%98512
>>
>> 6%108512
>>
>> 7%108512
>>
>> 8%108614
>>
>> 9%109614
>>
>> 10%109615
>>
>> 11%1010715
>>
>> 12%1010715
>>
>> 13%1110715
>>
>> 14%1110816
>>
>> 15%1110816
>>
>> 16%1110816
>>
>> 17%1110816
>>
>> 18%1110816
>>
>> 19%1210816
>>
>> 20%1210816
>>
>> So now I know, what percentile a person has, when she/ he scored a
>> certain amount of points (e.g. 6 points in "Punkte.AG1" = 0%).
>>
>> Here is my problem:
>>
>> I now want to write a function that assigns the percentile to the score
>> (for each task) and saves it in a new variable.
>>
>> So every person that scored 10 in "Punkte.AG1" gets a "12" in the new
>> variable "Percentile.AG1".
>>
>> Every person that scored 6 in "Punkte.AG1" gets a "6" in the new
>> variable "Percentile.AG1".
>>
>> The same thing should be done for the other tasks.
>>
>>
>> I new to R, so I don't have any clue, how to solve that. It would be
>> awesome, if you would know how to handle that.
>>
>> Thanks a lot!
>>
>> Anne
>>
>> --
>> M. Sc. Anne-Marie B. Gallrein
>> Technische Universitdt Dresden
>> Institut f|r Klinische, Diagnostische und Differentielle Psychologie
>> Diagnostik und Intervention
>> 01062 Dresden
>> Tel. +49 351 463-34004
>> gallrein at psychologie.tu-dresden.de
>>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org 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.
> ____________________________________________________________
> GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
> Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails
>
>


-- 
M. Sc. Anne-Marie B. Gallrein
Technische Universität Dresden
Institut für Klinische, Diagnostische und Differentielle Psychologie
Diagnostik und Intervention
01062 Dresden
Tel. +49 351 463-34004
gallrein at psychologie.tu-dresden.de


    [[alternative HTML version deleted]]


______________________________________________
R-help at r-project.org 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.




More information about the R-help mailing list