<html>
<head>
</head>
<body>
....many thanks - this would be a big step for advanced learning in prg.
R<br>
Christian<br>
<br>
<br>
<br>
Renaud Lancelot wrote:<br>
<blockquote type="cite" cite="mid:3CF38796.32C9C9F5@sentoo.sn">
<blockquote type="cite">
<pre wrap="">(2.problem) example:<br>id filterCriteria ratingOfSatisfaction ProductType<br>1 Man<br>1 60 A<br>1 40 B<br>3 Women<br>3 20 A<br>5 Man<br>5 40 A<br>5 100 B<br>5 80 C<br><br>I know that's no a perfect database model .<br></pre>
</blockquote>
<pre wrap=""><!----><br>Sure !<br><br></pre>
<blockquote type="cite">
<pre wrap="">But the dataset is much longer and now i have got a problem<br>i.e filter the ratingOfSatisfaction with gender!<br><br>Is there a possibilty to write a function in the really flexible R , which<br>autocount (copy the rows under the first row per ID for the filterCriteria)<br>until a new Id starts and again ....<br></pre>
</blockquote>
<pre wrap=""><!----><br>The following assumes the file is perfect (no missing value). However,<br>you will get an idea of what is possible to do. I have copied and pasted<br>the example above in a file called "file.txt":<br><br></pre>
<blockquote type="cite">
<pre wrap="">ProcessFile <- function(file){<br></pre>
</blockquote>
<pre wrap=""><!---->+ Line <- readLines(file)<br>+ Line <- tapply(X = seq(along = Line),<br>+ INDEX = seq(along = Line),<br>+ FUN = function(x, Line){<br>+ vec <- unlist(strsplit(x = Line[x], split = " "))<br>+ vec <- vec[vec != ""]<br>+ vec}, Line)<br>+ i <- 2; j <- 0<br>+ List <- list()<br>+ while(i < length(Line)){<br>+ xid <- Line[[i]][1]<br>+ xCrit <- Line[[i]][2]<br>+ i <- i + 1<br>+ while(Line[[i]][1] == xid & i < length(Line)){<br>+ j <- j + 1<br>+ List[[j]] <- data.frame(id = xid, Crit = xCrit,<br>+ Sat = as.numeric(Line[[i]][2]), Type = Line[[i]][3])<br>+ i <- i + 1<br>+ }<br>+ }<br>+ do.call("rbind", List)<br>+ }<br></pre>
<blockquote type="cite">
<pre wrap="">test <- ProcessFile(file = "d:\\analyses\\travail\\file.txt")<br>test<br></pre>
</blockquote>
<pre wrap=""><!----> id Crit Sat Type<br>1 1 Man 60 A<br>11 1 Man 40 B<br>12 3 Women 20 A<br>13 5 Man 40 A<br>14 5 Man 100 B<br>15 5 Man 80 C<br><br>Then:<br><br></pre>
<blockquote type="cite">
<pre wrap="">tapply(test$Sat, test$Crit, mean)<br></pre>
</blockquote>
<pre wrap=""><!----> Man Women <br> 64 20 <br></pre>
<blockquote type="cite">
<pre wrap="">tapply(X = test$Sat, test$Crit, table)<br></pre>
</blockquote>
<pre wrap=""><!---->$Man<br><br> 40 60 80 100 <br> 2 1 1 1 <br><br>$Women<br><br>20 <br> 1 <br><br></pre>
<blockquote type="cite">
<pre wrap="">tapply(as.factor(test$Sat), test$Crit, table)<br></pre>
</blockquote>
<pre wrap=""><!---->$Man<br><br> 20 40 60 80 100 <br> 0 2 1 1 1 <br><br>$Women<br><br> 20 40 60 80 100 <br> 1 0 0 0 0 <br><br>etc.<br><br>Hope this helps,<br><br>Renaud<br><br><br></pre>
</blockquote>
<br>
</body>
</html>