[R] Loop

jim holtman jholtman at gmail.com
Fri Nov 5 01:26:44 CET 2010


try this:

The top half of the matrix is the counts and the bottom is the value:

> x <- apply(mat, 2, function(a) c(sort(table(a)), as.integer(names(sort(table(a))))))
> x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 4856 4906 4857 4877 4788 4880 4861 4851 4878  4841
 [2,] 4907 4917 4863 4879 4853 4882 4922 4890 4907  4927
 [3,] 4942 4938 4930 4934 4951 4921 4935 4909 4912  4929
 [4,] 4943 4963 4951 4951 4962 4930 4939 4947 4944  4931
 [5,] 4952 4966 4956 4961 4965 4942 4948 4950 4972  4951
 [6,] 4973 4969 4970 4971 4974 4960 4965 4965 4981  4955
 [7,] 4977 4974 4979 4976 4983 4980 4965 4980 4985  4962
 [8,] 4980 4978 4985 4978 4994 4980 4986 4981 5000  4981
 [9,] 4986 4983 5001 4981 5004 4983 5003 4987 5003  4987
[10,] 4995 4989 5015 4996 5011 5005 5007 4988 5006  5001
[11,] 5000 4991 5016 5000 5015 5009 5017 4997 5015  5002
[12,] 5024 5010 5021 5012 5017 5012 5018 5015 5021  5021
[13,] 5030 5029 5022 5022 5017 5013 5022 5031 5023  5030
[14,] 5040 5031 5034 5027 5028 5015 5031 5033 5024  5031
[15,] 5046 5039 5041 5028 5028 5036 5045 5038 5033  5037
[16,] 5061 5040 5042 5028 5056 5039 5048 5045 5034  5038
[17,] 5066 5043 5042 5036 5068 5044 5051 5064 5035  5058
[18,] 5070 5043 5067 5054 5074 5074 5054 5085 5056  5058
[19,] 5074 5077 5080 5106 5090 5138 5057 5114 5073  5123
[20,] 5078 5114 5128 5183 5122 5157 5126 5130 5098  5137
[21,]   15    5   19    4   19   15    9    1   17    16
[22,]    3   11    5   20    2    8   20    2    9     8
[23,]    1   14   14   12   20   12   17   14    5    14
[24,]   16    4    7   17   15    2    3   18   18    10
[25,]    9   17   15   15    5    3    5   17   12    15
[26,]   10   18   13   13    9   10   11    4   20     2
[27,]    4   20   20   19    8   17   15   19   13    11
[28,]   18   13    4    7   17   19   13   11    3    12
[29,]   11   12   12    9    7   14   14    7    7     1
[30,]   19   16    9    3    3    9    4    6   15     5
[31,]   12    7    1   11   11    5   16   13    8    13
[32,]    2    3   18    5    4   11    8   16    4    18
[33,]    7   19   17    2   12    4   10    3   19     3
[34,]    8    1   10   16   10   18    2    8   10    17
[35,]   17   15   11   14   14    1    1   20   14    19
[36,]   20    8    6   18   13    6    6   10    1     9
[37,]   13    9   16   10   18    7    7   12    6     4
[38,]    6   10    2    6    6   16   12    9    2    20
[39,]   14    2    8    1    1   20   19    5   16     7
[40,]    5    6    3    8   16   13   18   15   11     6
>


2010/11/4 Matevž Pavlič <matevz.pavlic at gi-zrmk.si>:
> Hi Jim,
>
> Actually, this is better, but both values are what i am looking for. Count and the value of the count.
> Is there a way to just paste those two together?
>
> Thanks, m
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman at gmail.com]
> Sent: Thursday, November 04, 2010 9:59 PM
> To: Matevž Pavlič
> Cc: Petr PIKAL; r-help at r-project.org
> Subject: Re: [R] Loop
>
> Is this closer to what you want, assuming that it is the value of the most frequently occurring:
>
>> apply(mat, 2, function(x) head(names(sort(table(x), decreasing=T)),5))
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] "1"  "14" "5"  "1"  "4"  "14" "6"  "18" "11" "19"
> [2,] "3"  "3"  "13" "12" "3"  "11" "14" "9"  "18" "12"
> [3,] "2"  "18" "20" "8"  "11" "12" "17" "14" "14" "7"
> [4,] "5"  "11" "8"  "19" "5"  "18" "18" "15" "16" "10"
> [5,] "18" "13" "11" "11" "17" "3"  "4"  "16" "8"  "16"
>>
>
>
> 2010/11/4 Matevž Pavlič <matevz.pavlic at gi-zrmk.si>:
>> 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:petr.pikal at precheza.cz]
>> Sent: Thursday, November 04, 2010 4:56 PM
>> To: Matevž Pavlič
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Loop
>>
>> Hi
>>
>> r-help-bounces at r-project.org 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:petr.pikal at precheza.cz]
>>> Sent: Thursday, November 04, 2010 3:40 PM
>>> To: Matevž Pavlič
>>> Cc: r-help at r-project.org
>>> Subject: Re: [R] Loop
>>>
>>> Hi
>>>
>>> r-help-bounces at r-project.org 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:dwinsemius at comcast.net]
>>> > Sent: Wednesday, November 03, 2010 10:41 PM
>>> > To: Matevž Pavlič
>>> > Cc: r-help at r-project.org
>>> > 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:pburns at pburns.seanet.com]
>>> > > 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
>>> >
>>> > ______________________________________________
>>> > 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.
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list