Interpolation of decimal numbers in R

Asked

Viewed 163 times

1

Hello! I have a sequence of broken numbers (e.g. 191.1, 191.48, 191.87) and wanted to turn these numbers into integers (all 191. turn 191). Is it possible to do this in R or excel? I cannot do a simple function because there is no exact sequence of numbers (sometimes there are 3 broken numbers, sometimes there are only 2). Thank you!

2 answers

3

In R, you can use the function trunc

> trunc(191.1)
#[1] 191
> trunc(191.48)
#[1] 191
> trunc(191.8755)
#[1] 191

In Excel the function Truncate

= TRUNCAR(191,8755;0)

Dataset

   > dados
       wavelength spectra
#1      190.10  -13.10
#2      190.48  -10.30
#3      190.87   -3.55
#4      191.25   -0.82
#5      191.63   -4.14
#6      192.02   -1.06
#7      192.40    5.21
#8      192.78   12.23
#9      193.17   10.88
#10     193.55    5.95
#11     193.93    7.80
#12     194.31   13.10
#13     194.70   10.88

Average of each Spectra according to wavelength

library(dplyr)
dados %>% group_by(trunc(wavelength)) %>%
  summarise(media_spectra = mean(spectra))
# A tibble: 5 x 2
#  `trunc(wavelength)` media_spectra
#                <dbl>         <dbl>
#1                 190         -8.98
#2                 191         -2.48
#3                 192          5.46
#4                 193          8.21
#5                 194         12.0 
  • Thank you so much! It worked like this! But, what if for each fractional number I have a number associated with it (x, y) and I want that y also to be interpolated. To be clear, I have wavelength x reflected spectrum. The wavelength it’s fractionated, but I’m interested in the integer values. So I need to work with the average of the reflected spectra relative to each interpolation. It is possible with this same function?

  • It will be easier to help you if you post a sample or a minimally reproducible example of your data set. If your comic book is not too large, edit your previous post by placing a sample, for this you can use the command dput(dados), so it will be easier to understand what structure we are dealing with.

  • Of course! Here is a portion of my data Structure(list(wavelength = c(190.1, 190.48, 190.87, 191.25, 191.63, 192.02, 192.4, 192.78, 193.17, 193.55, 193.93, 194.31, 194.7), Spectra = c(-13.1, -10.3, -3.55, -0.82, -4.14, -1.06, 5.21, 12.23, 10.88, 5.95, 7.8, 13.1, 10.88)), Row.Names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame") ).

  • @Laísmoreira try like this: apply(trunc(dados), 2, mean)

  • But then I’ll average everything, right? The idea is to average all x = 190 and all y respective.

  • Would it then be a column for the other correct? Then do so: trunc(dados$wavelength)/trunc(dados$spectra)

  • Not yet... I don’t know how to explain it to you. I’ll try to explain it differently - for each wavelength there is a value of Pectra. But I don’t care about fractional wavelength values, so I want to interpolate the fractions. With this, I will have several equal wavelengths. The idea is I then average the spectras for each entire wavelength. So, I will have a column with wavelength and a column with Spectra. Does that make sense? I’m sorry I stalled on that question.

  • I think I understand now what you want, I’ll edit the answer given earlier, see if that’s exactly what it is.

  • It worked!! It was worth too much!!

Show 4 more comments

2

If you prefer, round these numbers to the nearest integer with the function round:

x<-c(191.1, 191.48, 191.87)
x
#[1] 191.10 191.48 191.87

round(x,0) # onde 0 é o número de casas decimais
#[1] 191 191 192

Browser other questions tagged

You are not signed in. Login or sign up in order to post.