- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
R Games / Fun
Tue 16 Feb 2010 - 12:49
Asxoloumai polla xronia me tin R kai pisteuo oti a kalyteros tropos gia na mathei kapoios kali R einai na prospathei na lysei mikra computational problimata kai na meletaei ton kodika allon. Gia tous xristes to forum pou tha tous endiefere kati tetoio ki exoun xrono skeftika na ftiakso ena thread me R (Olympic) Games, i.e., tha kano upload ena mikro problima (tis perissoteres fores motivated apo tin R-help) kai opoios endiaferetai mporei na proteinei mia R lysi (sto telos tha mporoume na kanoume discussion gia to poia theoteitai kalyteri).
Proto problima loipon:
I have a matrix with positive numbers, negative numbers, and NAs. An
example of the matrix is as follows
-1 -1 2 NA
3 3 -2 -1
1 1 NA -2
I need to compute a scaled version of this matrix. The scaling method is
dividing each positive numbers in each row by the sum of positive numbers
in that row and dividing each negative numbers in each row by the sum of
absolute value of negative numbers in that row.
So the resulting matrix would be
-1/2 -1/2 2/2 NA
3/6 3/6 -2/3 -1/3
1/2 1/2 NA -2/2
Is there an efficient way to do that in R? (hint: mporei na ginei me ena one-liner)
Proto problima loipon:
I have a matrix with positive numbers, negative numbers, and NAs. An
example of the matrix is as follows
-1 -1 2 NA
3 3 -2 -1
1 1 NA -2
I need to compute a scaled version of this matrix. The scaling method is
dividing each positive numbers in each row by the sum of positive numbers
in that row and dividing each negative numbers in each row by the sum of
absolute value of negative numbers in that row.
So the resulting matrix would be
-1/2 -1/2 2/2 NA
3/6 3/6 -2/3 -1/3
1/2 1/2 NA -2/2
Is there an efficient way to do that in R? (hint: mporei na ginei me ena one-liner)
Re: R Games / Fun
Tue 16 Feb 2010 - 14:44
Έφτιαξα και εγώ ένα πρόχειρο προγραμματάκι, μέσω πάρνηθας πάω βασικά αλλά ήταν το πρώτο που ήρθε στο νιονιό μ!
προυπόθεση ότι δεν υπάρχουν μηδενικές τιμές στο πίνακα. Υπάρχει κάποιος άλλος τρόπος να χειριστείς τα ΝΑ?
προυπόθεση ότι δεν υπάρχουν μηδενικές τιμές στο πίνακα. Υπάρχει κάποιος άλλος τρόπος να χειριστείς τα ΝΑ?
- Spoiler:
- Code:
xtest[is.na(xtest)]=0
for(i in 1:length(row.names(xtest))){
xarn=sum(abs(xtest[i,][xtest[i,]<0]))
xthet=sum(xtest[i,][xtest[i,]>0])
xtest[i,][xtest[i,]>0]=xtest[i,][xtest[i,]>0]/xthet
xtest[i,][xtest[i,]<0]=xtest[i,][xtest[i,]<0]/xarn
}
xtest[xtest==0]=NA
- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
Re: R Games / Fun
Wed 17 Feb 2010 - 14:35
Mia alli lysi einai:
- Code:
mat <- rbind(c(-1, -1, 2, NA), c(3, 3, -2, -1), c(1, 1, NA, -2))
mat / ave(abs(mat), row(mat), sign(mat), FUN = sum)
- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
Re: R Games / Fun
Sun 21 Feb 2010 - 8:20
Deutero problima:
Dedomenou oti exoume enan matrix, p.x.,
theloume na ton epanalaboume 50 fores, diladi
k.l.p., mexri na exoume 100 stiles.
Dedomenou oti exoume enan matrix, p.x.,
- Code:
mat <- matrix(c(1,4,3,6), 2, 2)
theloume na ton epanalaboume 50 fores, diladi
- Code:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 1 3 1 3
[2,] 4 6 4 6 4 6
k.l.p., mexri na exoume 100 stiles.
Re: R Games / Fun
Sun 21 Feb 2010 - 12:45
Κατι τετοιο δλδ...
- Code:
>matrix(rep(mat, 50), nrow(mat))
[,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]
[1,] 1 3 1 3 1 3 1 3 1 3 1 3
[2,] 4 6 4 6 4 6 4 6 4 6 4 6
[,99] [,100]
[1,] 1 3
[2,] 4 6
- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
Re: R Games / Fun
Sun 21 Feb 2010 - 13:10
Ontos auti einai mia apo tis pio efficient lyseis -- mia alli using do.call() einai:
- Code:
do.call(cbind, rep(list(mat), 50))
- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
Re: R Games / Fun
Mon 22 Feb 2010 - 21:11
Mia akoma lysi:
- Code:
mat <- matrix(c(1,4,3,6), 2, 2)
mat[, rep(seq_len(ncol(mat)), 50)]
- R4ever
- Posts : 44
Join date : 2010-01-20
Age : 44
Re: R Games / Fun
Tue 2 Mar 2010 - 16:42
Mia akomi mikri R exercise: exoume 11 times pou anikoun se 5 omades:
Theloume na kataskeuasoume olous tous dynatous syndiasmous 3 timon, opou kathe timi proerxetai apo diaforetiki omada.
- Code:
groups <- list(gp1 = 1:3, gp2 = 4:5, gp3 = 6:7,
gp4 = 8:10, gp5 = 11)
Theloume na kataskeuasoume olous tous dynatous syndiasmous 3 timon, opou kathe timi proerxetai apo diaforetiki omada.
Permissions in this forum:
You cannot reply to topics in this forum