Hi again,
Stil don't qute get it...
Here's what i did :
>mat<-read.csv("litologija.csv", dec=".", sep=";")
>apply(mat, 2, function(x) head(sort(table(x),decreasing=T),10))
With that i get a table(list/matrix...) which gives the highest count of
occurances of each value in a table (at least i think so)
But the problem is because it does not tell which value occurs the most (has
the highest count).
If written like this :
>apply(mat, 2, function(x) sort(table(x),decreasing=T))
I get decreasingly sorted values of counts of occurances of a specific field
and the value of that field for each column:
$W2
x
PEŠČEN GRADUIRAN IN PROD DO
GLINAST PROD, PREPEREL MELJAST GRUŠČ
GLINA Z MALO GRANULIRANA
1872 1542 552 519 458
214 175 174 132 114
62 53 47 45
ZELO PEŠČENA ZAGLINJEN KARBONATNI SKRILAVCA,
S SKRILAVCA GRANULIRAN PEČŠEN VEZAN
ZAOBLJEN GR. DROBEN SLABO
40 34 31 26 26
25 25 24 17 17
17 15 12 12
GRUŠČ, MELJASTO PEŠEEN DOBRO GRAN.
PEŠČENJAKA HUDOURNIŠKI MELJNA PEŠČN GIRADUIRAN
GLINAST, GOST GRADUTRAN GRANUL.
11 11 11 10 10
9 8 8 8 6
6 6 6 6
PESEK ZAMELJEN GRADUIPAN PREPEPEL PŠČEN
GPADUIRAN GRADUIRAN, GRADURAN POTOČNI PREPERL
SAVSKI CONA GLINASTEGA GRADUIRN
6 6 5 5 5
4 4 4 4 4
4 3 3 3
MELJAST, PEČEN PEŠČEN, PLASTI
DELNO GLINA, GLINASTO GRADUIAN GRADULRAN
GRDUIRAN GRUŠČ. KARB. KONGLOMERAT
3 3 3 3 2
2 2 2 2 2
2 2 2 2
KONGLOMERAT, MELJ NEKOLIKO OKER PESEK,
PEŠČCEN PEŠČEN. PLASTEH POD PPEPEREL
RPOD UMAZAN ZAOBLJEN, -
2 2 2 2 2
2 2 2 2 2
2 2 2 1
(GRUŠČ) (KARBONATNI) APNENCA DROBNOZRNAT, ENAKOMEREN
GBADUIRAN GLIANAST GLINASTA GPADUIRALN GPUŠČ
GRADAUIRAN GRADUIRA GRADUIRANPEŠČEN GRADUIRAU
But the first code somhove looses the acutal value of the field and just gives
the count
>apply(mat, 2, function(x) head(sort(table(x),decreasing=T),10))
VrtinaID ZapStev GlobinaOd GlobinaDo USCS Opis W1 W2 W3 W4 W5 W6
W7 W8 W9 W10 W11 W12 W13 W14 W15
[1,] 15 1248 282 290 2131 15 1820 1872 1677 1479 1441
1465 1261 769 848 1088 1490 1968 2459 2943 3408
[2,] 11 1119 198 235 1305 13 1791 1542 1495 1334 1317
1247 829 652 783 660 606 603 381 381 301
[3,] 11 1078 174 210 784 11 532 552 566 529 532
716 511 575 576 416 464 384 368 282 279
[4,] 11 835 147 173 691 11 471 519 390 351 358
571 364 521 556 381 398 352 287 282 259
[5,] 10 584 133 172 646 11 376 458 296 311 323
195 252 329 429 343 397 336 244 242 224
[6,] 10 389 123 142 386 10 253 214 237 268 310
130 233 265 376 263 378 258 228 210 205
[7,] 10 257 114 130 183 10 247 175 201 242 157
130 179 258 267 219 230 239 197 185 155
[8,] 9 198 105 126 148 9 135 174 157 170 146
102 163 213 266 215 221 188 197 179 155
[9,] 9 171 101 95 71 9 102 132 139 161 141
89 145 199 140 192 205 168 191 160 122
[10,] 9 144 94 91 31 8 93 114 124 158 121
84 136 183 134 185 160 158 181 155 115
How would i get the count and the actual value of the field in the same table
so that it would look something like this :
VrtinaID.count VrtinaID.value ZapStev.count ZapStev.value
[1,] 15 V-1 1248 ena ...
[2,] 11 V-12 1119 dva ...
.
.
.
Hope that explains it and thanks for help,
m
.
str(w)
-----Original Message-----
From: Petr PIKAL [mailto:[email protected]]
Sent: Thursday, November 04, 2010 4:56 PM
To: Matevž Pavlič
Cc: [email protected]
Subject: Re: [R] Loop
Hi
[email protected] napsal dne 04.11.2010 15:49:31:
> Hi all,
>
> I understand that you most of you this is a peice of cake but i am a
complete
> newbie in this....so any example would be greatly aprpeciated and also
any
> hint as how to get around in R. Frankly i sometimes see the help files
kinda confusing.
OK. Instead of
> > >> w1<-table(lit$W1)
> > >> w1<-as.data.frame(w1)
> > >> write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
> > >> w1<- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20)
Suppose you have data frame or matrix, and you want to have 5 most common
values from each column
# prepare matrix
x<-sample(1:20, 1000000, replace=T)
mat<-matrix(x, ncol=10)
#apply user defined function for each column
apply(mat, 2, function(x) head(sort(table(x), decreasing=T),5))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 5091 5135 5174 5133 5199 5097 5165 5157 5134 5068
[2,] 5073 5111 5143 5064 5113 5078 5102 5157 5131 5065
[3,] 5058 5092 5115 5051 5079 5064 5088 5128 5076 5063
[4,] 5056 5073 5114 5047 5059 5044 5037 5064 5071 5063
[5,] 5047 5064 5072 5041 5057 5041 5035 5058 5032 5061
If you want to do it in loop (can be quicker sometimes) and save it to
list make a list
lll<-vector("list", 10)
and fill it with your results
for (i in 1:10) lll[[i]]<-head(sort(table(mat[,i]), decreasing=T),5)
and now you can call values from this lll list simply by
lll[5]
[[1]]
9 15 13 6 16
5199 5113 5079 5059 5057
lll[[5]]
9 15 13 6 16
5199 5113 5079 5059 5057
or even
lll[[5]][3]
13
5079
without need for writing to individual files pasting together letters and
numbers etc.
There shall be R-intro document in your installation and it is worth
reading. It is not so big, you can manage it in less then month if you
complete more than 3 pages per day.
Regards
Petr
>
> M
>
> -----Original Message-----
> From: Petr PIKAL [mailto:[email protected]]
> Sent: Thursday, November 04, 2010 3:40 PM
> To: Matevž Pavlič
> Cc: [email protected]
> Subject: Re: [R] Loop
>
> Hi
>
> [email protected] napsal dne 04.11.2010 14:21:38:
>
> > Hi David,
> >
> > I am still having troubles with that loop ...
> >
> > This code gives me (kinda) the name of the column/field in a data
frame.
> Filed
> > names are form W1-W10. But there is a space between W and a number -->
> "W 10",
> > and column (field) names do not contain numbers.
> >
> > >for(i in 1:10)
> > >{
> > >vari <- paste("W",i)
> > >}
> > >vari
> >
> > [1] "W 10"
> >
> > Now as i understand than i would call different columns to R with
> >
> > >w<-lit[[vari]]
> >
> > Or am i wrong again?
> >
> > Then I would probably need another loop to create the names of the
> variables
> > on R, i.e. w1 to w10. Is that a general idea for the procedure?
>
> Beware of such loops. Instead of littering your workspace with
files/objects
> constructed by some paste(whatever, i) solution you can save results in
list
> or data.frame or matrix and simply use basic subsetting procedures or
lapply/
> sapply functions.
>
> I must say I never used such paste(...) construction yet and I work with
R for
> quite a long time.
>
> Regards
> Petr
>
>
> >
> >
> > Thank for the help, m
> >
> > -----Original Message-----
> > From: David Winsemius [mailto:[email protected]]
> > Sent: Wednesday, November 03, 2010 10:41 PM
> > To: Matevž Pavlič
> > Cc: [email protected]
> > Subject: Re: [R] Loop
> >
> >
> > On Nov 3, 2010, at 5:03 PM, Matevž Pavlič wrote:
> >
> > > Hi,
> > >
> > > Thanks for the help and the manuals. Will come very handy i am sure.
> > >
> > > But regarding the code i don't hink this is what i want....basically
> > > i
>
> > > would like to repeat bellow code :
> > >
> > > w1<-table(lit$W1)
> > > w1<-as.data.frame(w1)
> >
> > It appears you are not reading for meaning. Burns has advised you how
> > to
>
> > construct column names and use them in your initial steps. The `$`
> function is
> > quite limited in comparison to `[[` , so he was showing you a method
> that
> > would be more effective. BTW the as.data.frame step is unnecessary,
> since the
> > first thing write.table does is coerce an object to a data.frame. The
> > "write.table" name is misleading. It should be "write.data.frame". You
> cannot
> > really write tables with write.table.
> >
> > You would also use:
> >
> > file=paste(vari, "csv", sep=".") as the file argument to write.table
> >
> > > write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
> >
> > What are these next actions supposed to do after the file is written?
> > Are you trying to store a group of related "w" objects that will later
> be
> > indexed in sequence? If so, then a list would make more sense.
> >
> > --
> > David.
> >
> > > w1<- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20)
> > >
> > > 20 times, where W1-20 (capital letters) are the fields in a
> > > data.frame
>
> > > called "lit" and w1-20 are the data.frames being created.
> > >
> > > Hope that explains it better,
> >
> > > m
> > >
> > > -----Original Message-----
> > > From: Patrick Burns [mailto:[email protected]]
> > > Subject: Re: [R] Loop
> > >
> > > If I understand properly, you'll want something like:
> > >
> > > lit[["w2"]]
> > >
> > > instead of
> > >
> > > lit$w2
> > >
> > > more accurately:
> > >
> > > for(i in 1:20) {
> > > vari <- paste("w", i)
> > > lit[[vari]]
> > >
> > > ...
> > > }
> > >
> > > The two documents mentioned in my
> > > signature may help you.
> > >
> > > On 03/11/2010 20:23, Matevž Pavlič wrote:
> > >> Hi all,
> > >>
> > >> I managed to do what i want (with the great help of thi mailing
> > >> list) manually . Now i would like to automate it. I would probably
> > >> need a for loop for to help me with this...but of course I have no
> > >> idea how to do that in R. Bellow is the code that i would like to
> > >> be
>
> > >> replicated for a number of times (let say 20). I would like to
> > >> achieve that w1 would change to w2, w3, w4 ... up to w20 and by
> > >> that
>
> > >> create 20 data.frames that I would than bind together with cbind.
> > >>
> > >> (i did it like shown bellow -manually)
> > >>
> > >> w1<-table(lit$W1)
> > >> w1<-as.data.frame(w1)
> > >> write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".")
> > >> w1<- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20)
> > >>
> > >> w2<-table(lit$W2)
> > >>
> > >> w2<-as.data.frame(w2)
> > >>
> > >> write.table(w2,file="w2.csv",sep=";",row.names=T, dec=".")
> > >>
> > >> w2<- w2[order(w2$Freq, decreasing=TRUE),]
> > >>
> > >> w2<-head(w2, 20)
> > >> .
> > >> .
> > >> .
> > >>
> > >> Thanks for the help,m
> >
> > >
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> > ______________________________________________
> > [email protected] 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.
>
> ______________________________________________
> [email protected] 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.
______________________________________________
[email protected] 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.