【发布时间】:2015-11-08 06:40:10
【问题描述】:
我是 R 新手,需要帮助我在我目前正在处理的数据集中的一列中估算缺失值。下图显示了我想要估算的缺失值以及几列。
我想使用客户以前的条目为客户填写最小数量的值,因为我认为这最适合我的情况和数据。例如,在图像中,我应该能够用 1 填充缺失值(最小值为 1、5、2)。
在我的搜索过程中,我主要遇到了对给定类使用均值的方法,而不是最小值或最大值。
任何帮助或指点将不胜感激。
编辑:这是 dput 的输出。
structure(list(YEAR = c(2011L, 2012L, 2014L, 2015L, 2011L, 2012L
), CustomerId = c("00000063", "00000063", "00000063", "00000063",
"00000065", "00000065"), MemberType = structure(c(2L, 2L, 2L,
2L, 2L, 2L), .Label = c("GROUP", "INDIVIDUAL", "PARTNER"), class = "factor"),
MembershipTypeCode = structure(c(6L, 6L, 6L, 10L, 6L, 6L), .Label = c("EGROUP",
"EINDIV", "EINDIV2", "EPARTNER", "GROUP", "INDIV", "INDIV2",
"INDIV3", "PARTNER", "PLUS", "PLUS2", "PLUS20", "PLUS3",
"PLUSENTERPRI", "PLUSGROUP", "PLUSGROUP2", "PROF_ENTERPR",
"PROF_GROUP", "PROF_GROUP2", "PROF_INDIV", "PROF_INDIV2",
"PROF_INDIV3"), class = "factor"), MembershipPeriodBegin = structure(c(15279,
15677, 16071, 16436, 15006, 15371), class = "Date"), MembershipPeriodEnd = structure(c(15644,
16070, 16435, 16800, 15370, 15736), class = "Date"), ConsecutiveYearsAsMember = c(14L,
15L, 17L, 18L, 8L, 9L), AllocationUsage = c(0, 0, 0, 0, 0,
0), SetCOPPreference = structure(c(2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Y", "N"), class = "factor"), Purchase.Qty = c(2L,
5L, 1L, NA, 7L, 27L), Webcast.Registration = c(0L, 0L, 0L,
0L, 0L, 1L), Web.Visits = c(0L, 0L, 42L, 0L, 0L, 0L), Web.Page.Views = c(0L,
0L, 98L, 0L, 0L, 0L), Blog.Visits = c(0L, 0L, 3L, 0L, 0L,
0L), Blog.Page.Views = c(0L, 0L, 4L, 0L, 0L, 0L), Forum.Visits = c(0L,
0L, 45L, 0L, 0L, 0L), Forum.Page.Views = c(0L, 0L, 102L,
0L, 0L, 0L), ParatureTickets = c(0L, 0L, 0L, 0L, 0L, 0L),
ParatureChats = c(0L, 0L, 0L, 0L, 0L, 0L), Registered.for.Edu = c(0L,
0L, 0L, 0L, 0L, 0L), Attended.ICE = structure(c(2L, 2L, 2L,
2L, 2L, 2L), .Label = c("Y", "N"), class = "factor"), Attended.TK = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Y", "N"), class = "factor"),
Frugal = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Y",
"N"), class = "factor"), Chapter.Board = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Y", "N"), class = "factor"),
Retained = structure(c(5L, 5L, 5L, 1L, 5L, 5L), .Label = c("Active",
"Awaiting Renewal", "Future Dated", "Lost", "Retained"), class = "factor"),
ProfileCompletion = c(60, 60, 60, 60, 60, 60), NumberofLogins = c(1L,
1L, 15L, 0L, 0L, 4L), Downloads = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), ForumMember = structure(c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), .Label = "N", class = "factor"), FreeUpgrade = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Y", "N"), class = "factor")), .Names = c("YEAR",
"CustomerId", "MemberType", "MembershipTypeCode", "MembershipPeriodBegin",
"MembershipPeriodEnd", "ConsecutiveYearsAsMember", "AllocationUsage",
"SetCOPPreference", "Purchase.Qty", "Webcast.Registration", "Web.Visits",
"Web.Page.Views", "Blog.Visits", "Blog.Page.Views", "Forum.Visits",
"Forum.Page.Views", "ParatureTickets", "ParatureChats", "Registered.for.Edu",
"Attended.ICE", "Attended.TK", "Frugal", "Chapter.Board", "Retained",
"ProfileCompletion", "NumberofLogins", "Downloads", "ForumMember",
"FreeUpgrade"), row.names = c(NA, 6L), class = "data.frame")
谢谢,
普拉提克
【问题讨论】:
-
请编辑问题以插入
dput(head(data))的输出,以便任何人都可以使用数据集。我怀疑data$Purchase.Qty<-ave(data$Purchase.Qty,data$CustomerId,FUN=function(x) {x[is.na(x)]<-min(x,na.rm=TRUE);x})可能有效,但如果没有可重现的示例就无法测试。 -
它给了我以下警告信息'In min(x, na.rm = TRUE) : no non-missing arguments to min;返回 Inf'
标签: r missing-data