dplyr非常快,但我想知道我是否遗漏了一些东西:是否有可能总结出几个变量.例如:
library(dplyr)
library(reshape2)
(df=dput(structure(list(sex = structure(c(1L, 1L, 2L, 2L), .Label = c("boy",
"girl"), class = "factor"), age = c(52L, 58L, 40L, 62L), bmi = c(25L,
23L, 30L, 26L), chol = c(187L, 220L, 190L, 204L)), .Names = c("sex",
"age", "bmi", "chol"), row.names = c(NA, -4L), class = "data.frame")))
sex age bmi chol
1 boy 52 25 187
2 boy 58 23 220
3 girl 40 30 190
4 girl 62 26 204
dg=group_by(df,sex)
Run Code Online (Sandbox Code Playgroud)
使用这个小型数据帧,它很容易编写
summarise(dg,mean(age),mean(bmi),mean(chol))
Run Code Online (Sandbox Code Playgroud)
而且我知道,为了得到我想要的东西,我可以融化,获得手段,然后如dcast
dm=melt(df, id.var='sex')
dmg=group_by(dm, sex, variable); …Run Code Online (Sandbox Code Playgroud) 我正在使用以下方法使用fread将文件读入R:
fread("file:///C:/Users/Desktop/ads.csv")
fread("C:/Users/Desktop/ads.csv") # Just omitted "file:///"
Run Code Online (Sandbox Code Playgroud)
我观察到运行时非常不同:
microbenchmark(
fread("file:///C:/Users/Desktop/ads.csv"),
fread("C:/Users/Desktop/ads.csv")
)
Unit: microseconds
expr min lq mean median uq max neval cld
fread("file:///C:/Users/Desktop/ads.csv") 5755.975 6027.4735 6696.7807 6235.3365 6506.652 41257.476 100 b
fread("C:/Users/Desktop/ads.csv") 525.492 584.0215 673.7166 647.4745 727.703 1476.191 100 a
Run Code Online (Sandbox Code Playgroud)
为什么运行时变化如此之大?当我使用read.csv()时,两个变体之间没有明显的区别
当我在管道步骤中使用嵌套函数时,执行顺序似乎不直观.
df <- data.frame(a = c(1,NA,2), b = c(NA, NA, 1))
df %>% is.na %>% colSums # Produce correct count of missing values
df %>% colSums(is.na(.)) # Produce NA
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么第三行中的嵌套函数不能产生正确的结果?
x = 10
rm(x) # removed x from the environment
x = 10
x %>% rm() # Doesn't remove the variable x
Run Code Online (Sandbox Code Playgroud)
1)为什么管道技术不删除变量?
2)如何使用pipe和rm()来删除变量?
脚注:这个问题或许类似于magrittr包中的Pipe不适用于函数load()
我知道可以aws.s3 library在R中使用S3读取对象。如何找到对象大小without reading it into R?
我正在使用data.table包来聚合一个列,该列也是一个分组列.但结果并不是我的预期.
my_data = data.table(contnt=c("america", "asia", "asia","europe", "europe", "europe"), num= 1:6)
#my_data
#contnt num
#america 1
#asia 2
#asia 3
#europe 4
#europe 5
#europe 6
my_data[, length(contnt),by=contnt]
#contnt V1
#america 1
#asia 1
#europe 1
Run Code Online (Sandbox Code Playgroud)
当我聚合除分组列之外的列时,它的工作方式不同
my_data[, length(num),by=contnt]
#contnt V1
#america 1
#asia 2
#europe 3
Run Code Online (Sandbox Code Playgroud)
造成这种差异的原因是什么?
维基百科文章2016 年美国总统选举的页面信息包含“编辑历史记录”部分下的“页面创建日期”。如何通过 API 获取此日期?
如何从 Julia 的当前目录中删除文件?
file.remove()有没有像R中那样的直接函数?