Skip to content

Function, that assigns two vectors to each other

4 messages · Anne-Marie B. Gallrein, John Kane, arun

#
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
____________________________________________________________
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
#
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: