I have a variable of a df which contains numbers, but I need to transform it into decimals
An example:
[1] NA NA NA 595 595 5
so I need to create a new variable or transform it to get the following form
[1] NA NA NA 5.95 5.95 5.0
but I haven't find a way to do this, I'll appreciate any help
I have looking ways, but the only thing I find is a stata command
destring v1, g(new_var) dpcomma
drop v1
and I don't understand it
result from str
num [1:308535] NA NA NA 595 595 5 625 645 59 54 ...
an extract from dput
515, 555, 478, 665, 635, 555, 608, 578, 633, 62, 59, 608, 498,
603, 54, 638, 565, 625, NA, 545, 613, 573, 543, 653, 523, 568,
508, 635, 625, 598, 62, 565, 545, 655, 628, 52, 588, 598, 55,
598, 55, 578, 515, 64, 54, 54, 625, 565, 608, 588, 618, 535,
603, 663, 558, 638, 59, 518, 54, 613, 495, 535, 51, 543, 613,
583, 58, 593, 498, 568, 538, 525, 653, 555, 588, 56, 505, 54,
618, 56, 623, 508, 595, 6, 585, 518, 545, 525, 603, 588, 575,
568, 568, 588, 595, 593, 565, 535, 563, 643, 57, 645, 608, 608,
555, 57, 54, 555, 61, 528, 623, 653, 618, 528, 563, 63, 538,
52, 563, 51, 538, 525, 558, 568, NA, 523, 623, NA, 603, 555,
48, 633, 575, 583, 54, 578, 56, 65, 525, 5, 595, 663, 618, 59,
603, 525, 54, 555, 625, 528, 538, 67, 64, 528, 623, 615, 565,
548, 65, 508, 653, 63, 58, 553, 553, 548, 575, 553, 538, 583,
565, 613, 608, 68, 55, 605, 675, 585, 533, 565, 515, 525, 568,
CodePudding user response:
Something like this should work (this assumes your input is never 0, if this can happen you need to set Inf
values to 0 at the end of the process):
# first define a function that will to the job
toRate <- function(x) {
# get number of digits minus one
ndigits <- ceiling(log10(abs(x))) - 1 # or use floor and skip the -1
# divide by a power of 10 according to number of digits
y <- x / (10^ndigits)
return(y)
}
original <- c(515, 555, 478, 665, 635, 555, 608, 578, 633, 62, 59, 608, 498,
603, 54, 638, 565, 625, NA, 545, 613, 573, 543, 653, 523, 568,
508, 635, 625, 598, 62, 565, 545, 655, 628, 52, 588, 598, 55,
598, 55, 578, 515, 64, 54, 54, 625, 565, 608, 588, 618, 535,
603, 663, 558, 638, 59, 518, 54, 613, 495, 535, 51, 543, 613,
583, 58, 593, 498, 568, 538, 525, 653, 555, 588, 56, 505, 54,
618, 56, 623, 508, 595, 6, 585, 518, 545, 525, 603, 588, 575,
568, 568, 588, 595, 593, 565, 535, 563, 643, 57, 645, 608, 608,
555, 57, 54, 555, 61, 528, 623, 653, 618, 528, 563, 63, 538,
52, 563, 51, 538, 525, 558, 568, NA, 523, 623, NA, 603, 555,
48, 633, 575, 583, 54, 578, 56, 65, 525, 5, 595, 663, 618, 59,
603, 525, 54, 555, 625, 528, 538, 67, 64, 528, 623, 615, 565,
548, 65, 508, 653, 63, 58, 553, 553, 548, 575, 553, 538, 583,
565, 613, 608, 68, 55, 605, 675, 585, 533, 565, 515, 525, 568)
df <- cbind.data.frame(original = original)
df$new <- toRate(df$original)
Output:
df$new
[1] 5.15 5.55 4.78 6.65 6.35 5.55
[7] 6.08 5.78 6.33 6.20 5.90 6.08
[13] 4.98 6.03 5.40 6.38 5.65 6.25
[19] NA 5.45 6.13 5.73 5.43 6.53
[25] 5.23 5.68 5.08 6.35 6.25 5.98
[31] 6.20 5.65 5.45 6.55 6.28 5.20
[37] 5.88 5.98 5.50 5.98 5.50 5.78
[43] 5.15 6.40 5.40 5.40 6.25 5.65
[49] 6.08 5.88 6.18 5.35 6.03 6.63
[55] 5.58 6.38 5.90 5.18 5.40 6.13
[61] 4.95 5.35 5.10 5.43 6.13 5.83
[67] 5.80 5.93 4.98 5.68 5.38 5.25
[73] 6.53 5.55 5.88 5.60 5.05 5.40
[79] 6.18 5.60 6.23 5.08 5.95 6.00
[85] 5.85 5.18 5.45 5.25 6.03 5.88
[91] 5.75 5.68 5.68 5.88 5.95 5.93
[97] 5.65 5.35 5.63 6.43 5.70 6.45
[103] 6.08 6.08 5.55 5.70 5.40 5.55
[109] 6.10 5.28 6.23 6.53 6.18 5.28
[115] 5.63 6.30 5.38 5.20 5.63 5.10
[121] 5.38 5.25 5.58 5.68 NA 5.23
[127] 6.23 NA 6.03 5.55 4.80 6.33
[133] 5.75 5.83 5.40 5.78 5.60 6.50
[139] 5.25 5.00 5.95 6.63 6.18 5.90
[145] 6.03 5.25 5.40 5.55 6.25 5.28
[151] 5.38 6.70 6.40 5.28 6.23 6.15
[157] 5.65 5.48 6.50 5.08 6.53 6.30
[163] 5.80 5.53 5.53 5.48 5.75 5.53
[169] 5.38 5.83 5.65 6.13 6.08 6.80
[175] 5.50 6.05 6.75 5.85 5.33 5.65
[181] 5.15 5.25 5.68
CodePudding user response:
Another option is to insert a .
after the first character with stringr::str_sub
then convert back to numeric
.
x <- c(515, 555, 478, 665, 635, 555, 608, 578, 633, 62, 59)
stringr::str_sub(x, 2, 1) <- "."
as.numeric(x)
#----
[1] 5.15 5.55 4.78 6.65 6.35 5.55 6.08 5.78 6.33 6.20 5.90
Or with base function sub
as.numeric(sub("^(.{1})", "\\1\\.", x))
CodePudding user response:
You could do:
x/10^(nchar(x) - 1)
[1] 5.15 5.55 4.78 6.65 6.35 5.55 6.08 5.78 6.33 6.20 5.90