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.
Will this do it for you:> xX 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 # the the max value you want to start at > id_zero <- x$ID==0 # find zero values > # replace zero values with increasing numbers > x$ID[id_zero] <- cumsum(id_zero)[id_zero] + id_max > xX 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?
Had the test wrong in the last one' try this:> xX 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 > xX 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?
Sender: r-help-bounces at r-project.org On-Behalf-Of: rubenbauar at gmx.de Subject: Re: [R] Substitute problem Message-Id: <20136308.post at talk.nabble.com> Recipient: nalbicelli at tricadiacdpcmanagement.com -------------------------------------------------------- This information is being sent at the recipient's request or with their specific understanding. The recipient acknowledges that by sending this information via electronic means, there is no absolute assurance that the information will be free from third party access, use, or further dissemination. This e-mail contains information that is privileged and/or confidential and may be subject to legal restrictions and penalties regarding its unauthorized disclosure or other use. You are prohibited from copying, distributing or otherwise using this information if you are not the intended recipient. Past performance is not necessarily indicative of future results. This is not an offer of or the solicitation for any security which will be made only by private placement memorandum that may be obtained from the applicable hedge fund. If you have received this e-mail in error, please notify us immediately by return e-mail and delete this e-mail and all attachments from your system. Thank You. -------------- n?chster Teil -------------- Eine eingebundene Nachricht wurde abgetrennt... Von: Chris82 <rubenbauar at gmx.de> Betreff: Re: [R] Substitute problem Datum: Thu, 23 Oct 2008 10:52:25 -0700 Gr??e: 8764 URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081023/09f36cf9/attachment-0003.mht>