[R] Substitute problem

Chris82 rubenbauar at gmx.de
Wed Oct 22 15:06:27 CEST 2008


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.



More information about the R-help mailing list