Λέσχη Φίλων Στατιστικής - GrStats forum
R Games / Fun Forumgrstats

Join the forum, it's quick and easy

Λέσχη Φίλων Στατιστικής - GrStats forum
R Games / Fun Forumgrstats
Λέσχη Φίλων Στατιστικής - GrStats forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Για προβλήματα εγγραφής και άλλες πληροφορίες επικοινωνήστε με : grstats.forum@gmail.com ή grstats@stat-athens.aueb.gr

Go down
R4ever
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty 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)
paterask
paterask
Posts : 368
Join date : 2009-10-17
Location : Utrecht, The Netherlands
https://kpateras.eu

R Games / Fun Empty Re: R Games / Fun

Tue 16 Feb 2010 - 14:44
Message reputation : 50% (4 votes)
Έφτιαξα και εγώ ένα πρόχειρο προγραμματάκι, μέσω πάρνηθας πάω βασικά αλλά ήταν το πρώτο που ήρθε στο νιονιό μ!
προυπόθεση ότι δεν υπάρχουν μηδενικές τιμές στο πίνακα. Υπάρχει κάποιος άλλος τρόπος να χειριστείς τα ΝΑ?
Spoiler:
R4ever
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty 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
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty Re: R Games / Fun

Sun 21 Feb 2010 - 8:20
Deutero problima:

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.
mp83
mp83
Posts : 67
Join date : 2009-11-10
Location : Athens
http://statsravingmad.com/blog

R Games / Fun Empty 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
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty 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))
mp83
mp83
Posts : 67
Join date : 2009-11-10
Location : Athens
http://statsravingmad.com/blog

R Games / Fun Empty Re: R Games / Fun

Sun 21 Feb 2010 - 13:29
H do.call δεν ειναι στο toolbox μου (ακόμα...) Embarassed
R4ever
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty 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
R4ever
Posts : 44
Join date : 2010-01-20
Age : 44

R Games / Fun Empty Re: R Games / Fun

Tue 2 Mar 2010 - 16:42
Mia akomi mikri R exercise: exoume 11 times pou anikoun se 5 omades:

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.
Back to top
Permissions in this forum:
You cannot reply to topics in this forum