我有两个数据帧,其中一个称为“ students.short”,由以下生成:
students.short <- data.frame(shoesize=c(38,39,38,38,39,38,37,36),
population=c("kuopio","kuopio","kuopio","tampere",
"tampere","tampere","tampere","tampere"))
students.short
shoesize population
1 38 kuopio
2 39 kuopio
3 38 kuopio
4 38 kuopio
5 39 tampere
6 38 tampere
7 37 tampere
8 36 tampere
另一个叫做“ students.tall”:
students.tall <- data.frame(shoesize=c(44,42,43,43,42,44,43,43),
population=c("kuopio","kuopio","kuopio","kuopio",
"tampere","tampere","tampere","tampere"))
students.tall
shoesize population
1 44 kuopio
2 42 kuopio
3 43 kuopio
4 43 kuopio
5 42 tampere
6 44 tampere
7 43 tampere
8 43 tampere
并且我需要在总体(kuopio或tampere)与每个数据框的试穿尺寸的平均值之间创建一个交叉表
kuopio tampere
studenst.short 38.3 37.6
studenst.tall 43 43
请问有什么主意或帮助吗?我找不到一种干净或简便的方法?
答案 0 :(得分:1)
这是一个dplyr
驱动的答案。我们基本上首先使用.id
参数绑定两个数据帧,以区分数据帧。然后,我们group_by
,.id
和population
来计算平均值,即
library(dplyr)
bind_rows(df1, df2, .id = 'group') %>%
group_by(group, population) %>%
summarise(new = mean(shoesize))
给出,
# A tibble: 4 x 3 # Groups: group [?] group population new <chr> <fct> <dbl> 1 1 kuopio 38.3 2 1 tampere 37.6 3 2 kuopio 43 4 2 tampere 43
答案 1 :(得分:1)
使用_scrape_btn = ttk.Button(_mainframe, text='Scrape!', command=save, fg='blue')
_scrape_btn.grid(row=2, column=0, sticky=(N,E), pady=2)
_compress_btn = ttk.Button(_mainframe, text='Compress!', command=compress)
_compress_btn.grid(row=2, column=1, sticky=W, pady=2)
data.table
)setDT()
,使用名称作为ID(rbindlist()
)。idcol = TRUE
转换为宽格式,并与dcast
中的mean
进行汇总;
value.var
代码
shoesize
答案 2 :(得分:0)
首先使用docker stack
组合数据帧:
rbind()
然后使用df <- rbind(studnets.short, students.tall)
df$height_cat <- rep(c("short", "tall"), # create categorical height variable
c(nrow(students.short), nrow(students.tall)))
。对于此模拟数据框,其工作方式如下:
tapply()