3765 4

[问答] 关于R语言 reshape里的melt源代码 [推广有奖]

  • 0关注
  • 0粉丝

初中生

23%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
778 点
帖子
7
精华
0
在线时间
19 小时
注册时间
2015-8-11
最后登录
2016-12-18

楼主
调皮捣蛋大黑熊 发表于 2016-1-23 10:14:02 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

我想问一下,melt的源代码怎么获得啊?
我自己写了一个类似实现的方法,但是同样的数据量,自己的代码跑了2小时,melt嗖的一下。。。心里太不平衡了,想学习一下~~~
求好心人帮助!!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:reshape Shape R语言 ELT APE 源代码 好心人 平衡

回帖推荐

jiangbeilu 发表于2楼  查看完整内容

去页面下载:https://cran.r-project.org/web/packages/reshape/ reshape包代码。然后解压,在R文件夹里找到melt.r文件,打开就是原代码了。

沙发
jiangbeilu 学生认证  发表于 2016-1-23 14:16:14



  1. # Melt
  2. # Melt an object into a form suitable for easy casting.
  3. #
  4. # This the generic melt function. See the following functions
  5. # for specific details for different data structures:
  6. #
  7. # \itemize{
  8. #   \item \code{\link{melt.data.frame}} for data.frames
  9. #   \item \code{\link{melt.array}} for arrays, matrices and tables
  10. #   \item \code{\link{melt.list}} for lists
  11. # }
  12. #
  13. # @keyword manip
  14. # @arguments Data set to melt
  15. # @arguments Other arguments passed to the specific melt method
  16. melt <- function(data, ...) UseMethod("melt", data)

  17. # Default melt function
  18. # For vectors, make a column of a data frame
  19. #
  20. # @keyword internal
  21. melt.default <- function(data, ...) {
  22.   data.frame(value=data)
  23. }

  24. # Melt a list
  25. # Melting a list recursively melts each component of the list and joins the results together
  26. #
  27. # @keyword internal
  28. #X a <- as.list(1:4)
  29. #X melt(a)
  30. #X names(a) <- letters[1:4]
  31. #X melt(a)
  32. #X attr(a, "varname") <- "ID"
  33. #X melt(a)
  34. #X a <- list(matrix(1:4, ncol=2), matrix(1:6, ncol=2))
  35. #X melt(a)
  36. #X a <- list(matrix(1:4, ncol=2), array(1:27, c(3,3,3)))
  37. #X melt(a)
  38. #X melt(list(1:5, matrix(1:4, ncol=2)))
  39. #X melt(list(list(1:3), 1, list(as.list(3:4), as.list(1:2))))
  40. melt.list <- function(data, ..., level=1) {
  41.   var <- nulldefault(attr(data, "varname"), paste("L", level, sep=""))
  42.   names <- nulldefault(names(data), 1:length(data))
  43.   parts <- lapply(data, melt, level=level+1, ...)

  44.   namedparts <- mapply(function(x, name) {
  45.    x[[var]] <- name
  46.    x
  47.   }, parts, names, SIMPLIFY=FALSE)
  48.   do.call(rbind.fill, namedparts)
  49. }

  50. # Melt a data frame
  51. # Melt a data frame into form suitable for easy casting.
  52. #
  53. # You need to tell melt which of your variables are id variables, and which
  54. # are measured variables. If you only supply one of \code{id.vars} and
  55. # \code{measure.vars}, melt will assume the remainder of the variables in the
  56. # data set belong to the other. If you supply neither, melt will assume
  57. # factor and character variables are id variables, and all others are
  58. # measured.
  59. #
  60. # @arguments Data set to melt
  61. # @arguments Id variables. If blank, will use all non measure.vars variables.  Can be integer (variable position) or string (variable name)
  62. # @arguments Measured variables. If blank, will use all non id.vars variables. Can be integer (variable position) or string (variable name)
  63. # @arguments Name of the variable that will store the names of the original variables
  64. # @arguments Should NA values be removed from the data set?
  65. # @arguments Old argument name, now deprecated
  66. # @value molten data
  67. # @keyword manip
  68. # @seealso \url{http://had.co.nz/reshape/}
  69. #X head(melt(tips))
  70. #X names(airquality) <- tolower(names(airquality))
  71. #X melt(airquality, id=c("month", "day"))
  72. #X names(ChickWeight) <- tolower(names(ChickWeight))
  73. #X melt(ChickWeight, id=2:4)
  74. melt.data.frame <- function(data, id.vars, measure.vars, variable_name = "variable", na.rm = !preserve.na, preserve.na = TRUE, ...) {
  75.   if (!missing(preserve.na))
  76.     message("Use of preserve.na is now deprecated, please use na.rm instead")

  77.   var <- melt_check(data, id.vars, measure.vars)

  78.   if (length(var$measure) == 0) {
  79.     return(data[, var$id, drop=FALSE])
  80.   }

  81.   ids <- data[,var$id, drop=FALSE]
  82.   df <- do.call("rbind", lapply(var$measure, function(x) {
  83.     data.frame(ids, x, data[, x])
  84.   }))
  85.   names(df) <- c(names(ids), variable_name, "value")

  86.   df[[variable_name]] <- factor(df[[variable_name]], unique(df[[variable_name]]))

  87.   if (na.rm) {
  88.     df <- df[!is.na(df$value), , drop=FALSE]
  89.   }
  90.   rownames(df) <- NULL
  91.   df
  92. }

  93. # Melt an array
  94. # This function melts a high-dimensional array into a form that you can use \code{\link{cast}} with.
  95. #
  96. # This code is conceptually similar to \code{\link{as.data.frame.table}}
  97. #
  98. # @arguments array to melt
  99. # @arguments variable names to use in molten data.frame
  100. # @keyword manip
  101. # @alias melt.matrix
  102. # @alias melt.table
  103. #X a <- array(1:24, c(2,3,4))
  104. #X melt(a)
  105. #X melt(a, varnames=c("X","Y","Z"))
  106. #X dimnames(a) <- lapply(dim(a), function(x) LETTERS[1:x])
  107. #X melt(a)
  108. #X melt(a, varnames=c("X","Y","Z"))
  109. #X dimnames(a)[1] <- list(NULL)
  110. #X melt(a)
  111. melt.array <- function(data, varnames = names(dimnames(data)), ...) {
  112.   values <- as.vector(data)

  113.   dn <- dimnames(data)
  114.   if (is.null(dn)) dn <- vector("list", length(dim(data)))

  115.   dn_missing <- sapply(dn, is.null)
  116.   dn[dn_missing] <- lapply(dim(data), function(x) 1:x)[dn_missing]

  117.   char <- sapply(dn, is.character)
  118.   dn[char] <- lapply(dn[char], type.convert)
  119.   indices <- do.call(expand.grid, dn)

  120.   names(indices) <- varnames

  121.   data.frame(indices, value=values)
  122. }

  123. melt.table <- melt.array
  124. melt.matrix <- melt.array

  125. # Melt cast data.frames
  126. # Melt the results of a cast
  127. #
  128. # This can be useful when performning complex aggregations - melting
  129. # the result of a cast will do it's best to figure out the correct variables
  130. # to use as id and measured.
  131. #
  132. # @keyword internal
  133. melt.cast_df <- function(data, drop.margins=TRUE, ...) {
  134.   molten <- melt.data.frame(as.data.frame(data), id.vars=attr(data, "idvars"))

  135.   cols <- rcolnames(data)
  136.   rownames(cols) <- make.names(rownames(cols))

  137.   molten <- cbind(molten[names(molten) != "variable"], cols[molten$variable, , drop=FALSE])

  138.   if (drop.margins) {
  139.       margins <- !complete.cases(molten[,names(molten) != "value", drop=FALSE])
  140.     molten <- molten[!margins, ]
  141.   }

  142.   molten

  143. }

  144. # Melt cast matrices
  145. # Melt the results of a cast
  146. #
  147. # Converts to a data frame and then uses \code{\link{melt.cast_df}}
  148. #
  149. # @keyword internal
  150. melt.cast_matrix <- function(data, ...) {
  151.   melt(as.data.frame(data))
  152. }

  153. # Melt check.
  154. # Check that input variables to melt are appropriate.
  155. #
  156. # If id.vars or measure.vars are missing, \code{melt_check} will do its
  157. # best to impute them.If you only
  158. # supply one of id.vars and measure.vars, melt will assume the remainder of
  159. # the variables in the data set belong to the other. If you supply neither,
  160. # melt will assume character and factor variables are id variables,
  161. # and all other are measured.
  162. #
  163. # @keyword internal
  164. # @arguments data frame
  165. # @arguments Vector of identifying variable names or indexes
  166. # @arguments Vector of Measured variable names or indexes
  167. # @value id list id variable names
  168. # @value measure list of measured variable names
  169. melt_check <- function(data, id.vars, measure.vars) {
  170.   varnames <- names(data)
  171.   if (!missing(id.vars) && is.numeric(id.vars)) id.vars <- varnames[id.vars]
  172.   if (!missing(measure.vars) && is.numeric(measure.vars)) measure.vars <- varnames[measure.vars]

  173.   if (!missing(id.vars)) {
  174.     unknown <- setdiff(id.vars, varnames)
  175.     if (length(unknown) > 0) {
  176.       stop("id variables not found in data: ", paste(unknown, collapse=", "),
  177.         call. = FALSE)
  178.     }
  179.   }

  180.   if (!missing(measure.vars)) {
  181.     unknown <- setdiff(measure.vars, varnames)
  182.     if (length(unknown) > 0) {
  183.       stop("measure variables not found in data: ", paste(unknown, collapse=", "),
  184.         call. = FALSE)
  185.     }
  186.   }

  187.   if (missing(id.vars) && missing(measure.vars)) {
  188.     categorical <- sapply(data, function(x) class(x)[1]) %in% c("factor", "ordered", "character")
  189.     id.vars <- varnames[categorical]
  190.     measure.vars <- varnames[!categorical]
  191.     message("Using ", paste(id.vars, collapse=", "), " as id variables")
  192.   }

  193.   if (missing(id.vars)) id.vars <- varnames[!(varnames %in% c(measure.vars))]
  194.   if (missing(measure.vars)) measure.vars <- varnames[!(varnames %in% c(id.vars))]

  195.   list(id = id.vars, measure = measure.vars)
  196. }
复制代码
去页面下载:https://cran.r-project.org/web/packages/reshape/
reshape包代码。然后解压,在R文件夹里找到melt.r文件,打开就是原代码了。
已有 1 人评分论坛币 收起 理由
admin_kefu + 30 热心帮助其他会员

总评分: 论坛币 + 30   查看全部评分

Tomorrow is another day!

藤椅
ryoeng 在职认证  发表于 2016-1-23 14:31:00
提示: 作者被禁止或删除 内容自动屏蔽

板凳
调皮捣蛋大黑熊 发表于 2016-1-26 13:49:40
jiangbeilu 发表于 2016-1-23 14:16
去页面下载:https://cran.r-project.org/web/packages/reshape/
reshape包代码。然后解压,在R文件 ...
非常感谢啊!!!

报纸
调皮捣蛋大黑熊 发表于 2016-1-26 13:50:21
ryoeng 发表于 2016-1-23 14:31
也是另一个选择哦!
另外还想问一下,为啥,melt的效率能那么高呢??

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-6 10:04