Вы можете использовать функцию mapvalues
из пакета plyr
.
rescaleq<- function(x){
require(plyr)
if (length(x) != 30) stop("Vector of 30 elements required")
x[1]<- x[1]*10
x[c(2, 5, 6, 9)]<- mapvalues(x[c(2, 5, 6, 9)], from = 1:6, to = seq(100, 0, by = -20))
x[c(3,4,7,8,10,11,12,13,16,17,18)]<- mapvalues(x[c(3,4,7,8,10,11,12,13,16,17,18)], from = 1:6, to = seq(0, 100, by = 20))
x[c(14, 25, 26, 27, 28, 29, 30)]<- mapvalues(x[c(14, 25, 26, 27, 28, 29, 30)], from = 1:5, to = seq(100, 0, by = -25))
x[c(19, 20)]<- mapvalues(x[c(19, 20)], from = 1:5, to = seq(0, 100, by = 25))
x[c(5, 21, 23, 24)]<- mapvalues(x[c(5, 21, 23, 24)], from = 1:4, to = seq(0, 100, length.out = 4))
x[22]<- mapvalues(x[22], from = 1:3, to = seq(0, 100, by = 50))
return(round(x, 2))
}
И проверить ее с некоторыми данными:
> xvector <- sample.int(3, 31, replace=T)
> xvector
# [1] 2 1 3 2 2 3 2 1 1 3 1 3 1 1 1 1 2 1 3 1 1 2 1 1 2 2 3 1 3 3
> rescaleq(xvector[-31]) # Note that below, these are messages NOT errors or warnings
#The following `from` values were not present in `x`: 4, 5, 6
#The following `from` values were not present in `x`: 4, 5, 6
#The following `from` values were not present in `x`: 4, 5
#The following `from` values were not present in `x`: 2, 4, 5
#The following `from` values were not present in `x`: 3, 4
#The following `from` values were not present in `x`: 1, 3
# [1] 20.00 100.00 80.00 60.00 100.00 40.00 20.00 20.00 0.00 40.00 0.00 40.00
#[13] 0.00 0.00 20.00 0.00 100.00 75.00 75.00 50.00 100.00 50.00 50.00 50.00
#[25] 0.00 33.33 0.00 0.00 0.00 50.00
Если вы хотите удалитьсообщения, сгенерированные mapvalues
, попробуйте обернуть вокруг них suppressMessages
, например suppressMessages(mapvalues(x[c(2, 5, 6, 9)], from = 1:6, to = seq(100, 0, by = -20)))
и т. д.