您的位置:首页 > 其它

RNote__Reshape2

2015-06-26 20:01 260 查看
melt

parse_formula

cast

recast

colsplit

分解到重组:melt——cast

melt

融化数据,变为长格式;

为泛型函数,具体有:

melt.data.frame for data.frames

melt.array for arrays, matrices and tables

melt.list for lists

## S3 method for class ' data.frame '
melt(data, id.vars, measure.vars,
variable.name = "variable", ..., na.rm = FALSE, value.name = "value",
factorsAsStrings = TRUE)


id.vars是分类关键词,measure.vars是在数据data中只选择部分变量(列)输出。

parse_formula

将字符串转化为cast中的可执行formula。

reshape2:::parse_formula("a + ...", letters[1:6])
reshape2:::parse_formula("a ~ b + d")


cast

浇铸,重组数据。

dcast(data, formula, fun.aggregate = NULL, ..., margins = NULL,
subset = NULL, fill = NULL, drop = TRUE,
value.var = guess_value(data))

acast(data, formula, fun.aggregate = NULL, ..., margins = NULL,
subset = NULL, fill = NULL, drop = TRUE,
value.var = guess_value(data))


> recast(french_fries, time ~ variable, id.var = 1:4)
Aggregation function missing: defaulting to length(72代表长度)
time potato buttery grassy rancid painty
1     1     72      72     72     72     72
2     2     72      72     72     72     72
3     3     72      72     72     72     72
4     4     72      72     72     72     72
5     5     72      72     72     72     72
6     6     72      72     72     72     72
7     7     72      72     72     72     72
8     8     72      72     72     72     72
9     9     60      60     60     60     60
10   10     60      60     60     60     60


formula:a~b~c,其中c是最外围维度,b表示列,a表示行。

recast

将melt与cast一步完成;

recast(data, formula, ..., id.var, measure.var)


colsplit

> x <- c("a_1", "a_2", "b_2", "c_3")
> vars <- colsplit(x, "_", c("trt", "time"))
> vars
trt time
1   a    1
2   a    2
3   b    2
4   c    3
> str(vars)
'data.frame':   4 obs. of  2 variables:
$ trt : chr  "a" "a" "b" "c"
$ time: int  1 2 2 3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  R note 数据整理