Reprogramming a semi-advanced rounding program

Hello everyone: I have a question related to the previous message about the rounding program (available here: R: combine the contents inside each line so that the total amount is equal to the number that I specify ).

The program is designed to round the contents within each line, so the total amount of lines is equal to the number that I specify. Copied from the original post, here is the MWE that works:

Round <- function(x, target) {
r.x <- round(x)
diff.x <- round(x) - x
if ((s <- sum(r.x)) == target) {
return(r.x)
} else if (s > target) {
select <- seq(along=x)[diff.x > 0]
which <- which.max(diff.x[select])
x[select[which]] <- r.x[select[which]] - 1
Round(x, target)
} else {
select <- seq(along=x)[diff.x < 0]
which <- which.min(diff.x[select])
x[select[which]] <- r.x[select[which]] + 1
Round(x, target)
}
}

dat <- read.table(header = TRUE, row.names = paste0('place', 1:4),
              text="race1 race2 total
                    1.2  2.1  3.4
                    3.4  3.6  7.0
                    7.7  0.8  8.5
                    5.3  1.4  6.7")

totals <- c(4.0, 7.0, 8.0, 7.0)

do.call(rbind, lapply(1:nrow(dat), function(x) Round(dat[x, -3], totals[x])))

#        race1 race2
# place1     2     2
# place2     3     4
# place3     7     1
# place4     5     2

: , , , , 1 . , 2 3 4. , , 6-8 2, . 6 8, .

, "Round" , :

dat <- read.table(header = TRUE, row.names = paste0('place', 1:4),
              text="race1 race2 total
                    1.2  2.1  3.4
                    3.4  3.6  7.0
                    7.7  0.8  8.5
                    5.3  1.4  6.7")

totals <- c(4.0, 5.0, 8.0, 7.0)

do.call(rbind, lapply(1:nrow(dat), function(x) Round(dat[x, -3], totals[x])))
Error in data.frame(value, row.names=rn, check.names = FALSE, check.rows
= FALSE) : 'row.names' should specify one of the variables

( c (4.0, 7.0,...) c (4.0, 5.0,...))

, , , , , , , 4 . ( data.frame , 14 .)

, rawr, , , 3 .

:

Round <- function(x, target){
 r.x <- round(x)
 diff.x <- round(x) - x
 if ((s <- sum(r.x)) == target) {return(r.x)
} else if (s > target) {
     select <- seq(along=x)[diff.x != 0]
     which <- which.max(diff.x[select])
     x[select[which]] <- r.x[select[which]] - 1
     Round(x, target)
 }
 else{
     select <- seq(along=x)[diff.x != 0]
     which <- which.min(diff.x[select])
     x[select[which]] <- r.x[select[which]] + 1
     Round(x, target)
 }
}

, :

dat <- read.table(header = TRUE, row.names = paste0('district', 1:4),
text="race1 race2 total
1.2 2.1 3.4
3.4 3.6 7.0
7.7 0.8 8.5
5.3 1.4 6.7")

totals <- c(4.0, 5.0, 12.0, 7.0)

do.call(rbind, lapply(1:nrow(dat), function(x) Round(dat[x, -3], totals[x])))

Error in data.frame(value, row.names = rn, check.names = FALSE, check.rows = FALSE) : 
'row.names' should specify one of the variables
+2
1

- , , .

, , , , , diff.x != 0 FALSE, which.max .

, , , - , /.

min max , . , . set.seed, , . , .

Round <- function(x, target) {
  r.x <- round(x)
  diff.x <- r.x - x
  if ((s <- sum(r.x)) == target) {
    return(r.x)
  } else if (s > target) {
    select <- seq_along(x)[diff.x != 0]
    select <- if (length(select)) select else which.max(x)
    wh <- which.max(diff.x[select])
    x[select[wh]] <- r.x[select[wh]] - 1
    Recall(x, target)
  } else {
    select <- seq_along(x)[diff.x != 0]
    select <- if (length(select)) select else which.min(x)
    wh <- which.min(diff.x[select])
    x[select[wh]] <- r.x[select[wh]] + 1
    Recall(x, target)
  }
}

Round2 <- function(x, target) {
  set.seed(1)
  r.x <- round(x)
  diff.x <- r.x - x
  if ((s <- sum(r.x)) == target) {
    return(r.x)
  } else if (s > target) {
    select <- seq_along(x)[diff.x != 0]
    # select <- if (length(select)) select else which.max(x)
    select <- if (length(select)) select else
      sample(seq_along(x), 1, prob = prop.table(x))
    wh <- which.max(diff.x[select])
    x[select[wh]] <- r.x[select[wh]] - 1
    Recall(x, target)
  } else {
    select <- seq_along(x)[diff.x != 0]
    # select <- if (length(select)) select else which.min(x)
    select <- if (length(select)) select else
      sample(seq_along(x), 1, prob = prop.table(x))
    wh <- which.min(diff.x[select])
    x[select[wh]] <- r.x[select[wh]] + 1
    Recall(x, target)
  }
}

dat <- read.table(header = TRUE, row.names = paste0('district', 1:4),
                    text="race1 race2 total
1.2 2.1 3.4
3.4 3.6 7.0
7.7 0.8 8.5
5.3 1.4 6.7")

totals <- c(4.0, 5.0, 12.0, 7.0)

, , , 7.7 โ€‹โ€‹ 9 0.8 8

cbind(
  dat,
  totals,
  do.call(rbind, lapply(1:nrow(dat), function(x) Round(dat[x, -3], totals[x])))
)

#           race1 race2 total totals race1 race2
# district1   1.2   2.1   3.4      4     2     2
# district2   3.4   3.6   7.0      5     2     3
# district3   7.7   0.8   8.5     12     9     3
# district4   5.3   1.4   6.7      7     5     2


cbind(dat[3, ], Round(dat[3, 1:2], 17))
#           race1 race2 total race1 race2
# district3   7.7   0.8   8.5     9     8

, , , 7.7 15 0,8 2, , .

cbind(
  dat,
  totals,
  do.call(rbind, lapply(1:nrow(dat), function(x) Round2(dat[x, -3], totals[x])))
)

#           race1 race2 total totals race1 race2
# district1   1.2   2.1   3.4      4     2     2
# district2   3.4   3.6   7.0      5     2     3
# district3   7.7   0.8   8.5     12    10     2
# district4   5.3   1.4   6.7      7     5     2

cbind(dat[3, ], Round2(dat[3, 1:2], 17))

#           race1 race2 total race1 race2
# district3   7.7   0.8   8.5    15     2
+1

All Articles