按R中的最后一个可用值推断

时间:2016-04-08 11:45:57

标签: r

我的数据看起来像这样

> data
   ID Price
1   1     1
2   2     3
3   3    NA
4   4    NA
5   5     7
6   6     6
7   7    NA
8   8    NA
9   9    NA
10 10    10

我想根据可用的最后一个值推断值,以便我的数据看起来像这样

> data_final
   ID Price
1   1     1
2   2     3
3   3     3
4   4     3
5   5     7
6   6     6
7   7     6
8   8     6
9   9     6
10 10    10

非常感谢任何帮助

3 个答案:

答案 0 :(得分:4)

我们可以使用CPS.update({ "complaints._id": request.params.documentid }, { "$set": { "complaints.$.status": "WAITING", "complaints.$.color": "#CE002D" } }, function(err, booking) { Booking.findById(id, function(err, newbooking) { if (err) { console.log('Errorrrr Occured' + JSON.stringify(err)); return reply(JSON.stringify(err)).code(500); } else { console.log('All Booking are---->>' + booking); return reply(newbooking).code(200); } }); });

na.locf

答案 1 :(得分:1)

您可以在tidyr

中使用fill
library(tidyr)
> fill(df, Price)
ID Price
1   1     1
2   2     3
3   3     3
4   4     3
5   5     7
6   6     6
7   7     6
8   8     6
9   9     6
10 10    10

答案 2 :(得分:0)

这是基础R中的方法:

# construct data.frame
data <- data.frame("ID"=1:10, "Price"=c(1,3,NA,NA,7,6,NA,NA,NA,10))
# loop through vector iteratively replacing subsets of missing values
while(any(is.na(data$Price))) {
  data[, "Price"][is.na(data$Price)] <- data[, "Price"][which(is.na(data$Price))-1]