[R] Substitute problem

jim holtman jholtman at gmail.com
Thu Oct 23 03:57:33 CEST 2008


Had the test wrong in the last one'  try this:

> x
  X     value     row     col ID         n
1 1 11.105009 5511625 3463000  1 -619.1125
2 2 10.324148 5499625 3465000  0    1.0000
3 3  8.744591 5503625 3475000  0    1.0000
4 4  9.523473 5494625 3475000  3 -578.2354
5 5  8.856097 5507625 3480000  4 -619.1125
6 6  8.636881 5514625 3497000  5 -140.8012
> id_max <- 5
> id_change <- (x$ID == 0) & (x$n > 0)
> x$ID[id_change] <- cumsum(id_change)[id_change] + id_max
> x
  X     value     row     col ID         n
1 1 11.105009 5511625 3463000  1 -619.1125
2 2 10.324148 5499625 3465000  6    1.0000
3 3  8.744591 5503625 3475000  7    1.0000
4 4  9.523473 5494625 3475000  3 -578.2354
5 5  8.856097 5507625 3480000  4 -619.1125
6 6  8.636881 5514625 3497000  5 -140.8012
>


On Wed, Oct 22, 2008 at 9:06 AM, Chris82 <rubenbauar at gmx.de> wrote:
>
> hello R users,
>
> I didn't find a solution for a special problem.
> I have two dataframes.
>
> dataframe1:
>
>  X        value         row             col                       ID
>  1  8.973498062         5512625         3460000         1
>  2 11.656658570         5501625         3464000         2
>  3 11.121777570         5495625         3473000         3
>  4  9.310465964         5508625         3477000         4
>  5  8.883483845         5515625         3496000         5
>
>
> dataframe2:
>
>  X        value         row             col               ID                   n
>  1 11.105009400         5511625 3463000                   1             -619.112472616732
>  2 10.324148150         5499625 3465000                     0           1.000000000000
>  3  8.744590903         5503625 3475000                   0             1.000000000000
>  4  9.523473123         5494625 3475000                   3          -578.235381588725
>  5  8.856097133         5507625 3480000                     4        -619.112472616732
>  6  8.636881453         5514625 3497000                   5          -140.801233634174
>
> Now I want if column "n" in dataframe2 is greater than 0 column "ID" which
> is 0 is now maxium column "ID" in dataframe1 +1 and the for the second 0
> maximum + 2
>
> the finished dataframe2 should look like this:
>
>  X        value         row             col                       ID           n
>  1 11.105009400         5511625 3463000                   1       -619.112472616732
>  2 10.324148150         5499625 3465000                     6     1.000000000000
>  3  8.744590903         5503625 3475000                   7       1.000000000000
>  4  9.523473123         5494625 3475000                   3     -578.235381588725
>  5  8.856097133         5507625 3480000                     4   -619.112472616732
>  6  8.636881453         5514625 3497000                   5     -140.801233634174
>
> My idea was below:
>
> lastrow1 <- length(dataframe2[,1])
> lastrow2 <- length(dataframe1[,1])
> anz <- sum(dataframe2[,6] > 0)
>
>
> for (k in 1:anz){
> for (i in 1:lastrow1){
> for (j in 1:lastrow2){
> if (dataframe2[i,6] > 0){
> dataframe2[i,5] <- max(dataframe1[j,5])+(k-k+1)
> }
> }
> }
> }
>
>
> but the result is:
>
>  X        value         row             col                       ID                   n
>  1 11.105009400         5511625 3463000                   1       -619.112472616732
>  2 10.324148150         5499625 3465000                     6      1.000000000000
>  3  8.744590903         5503625 3475000                   6        1.000000000000
>  4  9.523473123         5494625 3475000                   3     -578.235381588725
>  5  8.856097133         5507625 3480000                     4   -619.112472616732
>  6  8.636881453         5514625 3497000                   5     -140.801233634174
>
> R gives me the right value in the second row of "ID" with "6" but the third
> row of "ID" is also "6" but should be "7"
>
> any ideas?
>
> thanks!
> --
> View this message in context: http://www.nabble.com/Substitute-problem-tp20110333p20110333.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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?



More information about the R-help mailing list