是否可以在删除值和索引后保留它们? 我的意思是我很想测试某些插值方法,我想删除一些值,运行插值,然后将创建的新值与我之前删除的值进行比较。因此,我不会出现随机删除某些值的问题,但是我不知道如何继续保留它们。我希望我的问题很清楚,
最诚挚的问候
马克西姆
答案 0 :(得分:0)
我认为,对该问题的最佳答案是挑战其前提的一个答案:您不必删除插值即可进行另一种形式的插值。一个例子:
xvals <- 1:10 # These are the observed x values
yvals <- sin(xvals) # The observed y values
linear_interpolation <- approx(xvals, yvals) # linear interpolation of values
spline_interpolation <- spline(xvals, yvals, n = 50) # spline interpolation
# We can collect the interpolation results in a dataframe for easy comparison
comparison <- data.frame(x = linear_interpolation$x,
linear_interpolation = linear_interpolation$y,
spline_interpolation = spline_interpolation$y)
head(comparison) # Look at the first few rows
#> x linear_interpolation spline_interpolation
#> 1 1.000000 0.8414710 0.8414710
#> 2 1.183673 0.8539289 0.9476460
#> 3 1.367347 0.8663868 1.0066238
#> 4 1.551020 0.8788447 1.0227805
#> 5 1.734694 0.8913027 1.0004924
#> 6 1.918367 0.9037606 0.9441358
# We can also plot them to compare visually
# I like to use the colorblind-friendly palette from
# Wong, Bang. 2011. "Points of view: Color blindness." Nature Methods 8:441.
wong_palette <- c("#e69f00", "#56b4e9", "#009e73")
# Start with the observed values
plot(xvals, yvals, pch = 19, xlab = "X Values", ylab = "Y Values",
main = "Interpolation Comparison")
# Plot the linear interpolation
lines(linear_interpolation, col = wong_palette[1])
# The spline interpolation
lines(spline_interpolation, col = wong_palette[2])
# And, since we know the true function in this simulation, the true function
lines(linear_interpolation$x, sin(linear_interpolation$x), col = wong_palette[3])
# Finish with a legend so we know what we're looking at
legend("bottomleft", seg.len = 1, lty = 1, bty = "n", col = wong_palette,
legend = c("Linear Interp.", "Spline Interp.", "True Function"))
由reprex package(v0.2.1)于2018-11-18创建
答案 1 :(得分:0)
最后我确实喜欢这个(不是很传统,但是..):
data_test<-data.frame(
x<-1:10,
y<-1:10
)
data_test
data_test$y [sample(nrow(data_test),2)]<- NA
data_test
index<-which(data_test$y %in% c(NA))
index
store_data<-data_test[index,]
data_testnew<- na.omit(data_test)
data_testnew