Note that there are some explanatory texts on larger screens.

plurals
  1. POchange a variable value according to others variables values in R
    primarykey
    data
    text
    <p>I want to change(or keep) a value of my dataframe according to the values of other variables in the same data frame. I've been looking for and answer without much success. This is an example of the database I have:</p> <pre><code> date day VM id 12/01/2013 4 133 1 12/01/2013 4 163 1 13/01/2013 5 143 1 13/01/2013 5 123 1 14/01/2013 6 90 1 14/01/2013 6 190 1 15/01/2013 7 922 1 15/01/2013 7 952 1 16/01/2013 1 13 1 16/01/2013 1 25 1 17/01/2013 2 333 1 17/01/2013 2 123 1 18/01/2013 3 143 1 18/01/2013 3 169 1 19/01/2013 4 203 1 19/01/2013 4 133 1 </code></pre> <p>each number represents a day (1:Monday, 2:tuesday,...)and each day has hundreds of data, also the information is for 8 days so there will be one day repeated ( but in a different date). When I apply summary it gives me the info mixing the data of the repeated day and I want to avoid that, My idea is to change the last day of use (the repeated one) to the number 8 but the repeated day change by id and I've got plenty of data. So far I've tried this:</p> <pre><code>unicos&lt;-unique(data$id) data_corr&lt;-NULL for(j in 1:length(unicos)) subset&lt;-data[which(data$id==unicos[j]),] subset$day1&lt;-NULL times&lt;-0 vector&lt;-NULL for(i in 1:(dim(subset)[1])){ if((subset$day[i]&lt;-subset$day[1]) &amp;&amp; if (subset$date[i]&lt;-subset$date[1])){ vector&lt;-c(vector, i) times&lt;-times+1 subset$day1[i]&lt;-subset$day[1] } subset$day1&lt;-8 } Basal_corr&lt;-rbind(Basal_corr, subset) } </code></pre> <p>The first "for" is meant to apply the conditions one id at the time. The second part is the hard one. What I want to do is that if the day in the row [i] is in, is the same that the one in the first row (since it is the one that is going to repeat) then go check the date, if the date is the same too I want to keep keep the original day value in a new column called day1, and if the date is different then day1 value will be 8. I use Basal_corr&lt;-rbind(Basal_corr, subset) to keep track of all the changes made by id. I get an "ERROR" everytime I try to run it, and I really don't know whats grong I'm an R newbie and this is beyond what I know till now.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload