我对数据帧有很多操作,我想加快使用mclapply()或其他lapply()类似的功能.我最容易解决这个问题的方法之一是使数据帧的每一行成为列表中的一个小数据帧.我可以很容易地做到这plyr一点:
df <- data.frame( a=rnorm(1e4), b=rnorm(1e4))
require(plyr)
system.time(myList <- alply( df, 1, function(x) data.frame(x) ))
Run Code Online (Sandbox Code Playgroud)
一旦我将数据作为列表,我可以轻松地执行以下操作:
mclapply( myList, function(x) doSomething(x$a) )
Run Code Online (Sandbox Code Playgroud)
这可以游泳,但我有很多数据,adply()步骤很慢.我尝试在adply步骤中使用多核并行后端,但它从未使用过多个处理器,即使我已经注册了8.我很怀疑并行选项可能无法解决这类问题.
关于如何加快速度的任何提示?也许基础R解决方案?
我有定期运行回归的数据.每个"数据块"的数据都适合不同的回归.例如,每个州可能具有解释从属值的不同功能.这似乎是典型的"拆分 - 应用 - 组合"类型的问题,因此我使用的是plyr包.我可以轻松创建一个lm()运行良好的对象列表.但是,我不能完全理解我以后如何使用这些对象来预测单独data.frame中的值.
这是一个完全人为的例子,说明了我正在尝试做的事情:
# setting up some fake data
set.seed(1)
funct <- function(myState, myYear){
rnorm(1, 100, 500) + myState + (100 * myYear)
}
state <- 50:60
year <- 10:40
myData <- expand.grid( year, state)
names(myData) <- c("year","state")
myData$value <- apply(myData, 1, function(x) funct(x[2], x[1]))
## ok, done with the fake data generation.
require(plyr)
modelList <- dlply(myData, "state", function(x) lm(value ~ year, data=x))
## if you want to see the summaries of the lm() do …Run Code Online (Sandbox Code Playgroud) 给出一个清单:
alist = list(
list(name="Foo",age=22),
list(name="Bar"),
list(name="Baz",age=NULL)
)
Run Code Online (Sandbox Code Playgroud)
什么是将其转换为具有名称和年龄列的数据框的最佳方式,具有缺失值(我将按优先顺序接受NA或"")?
使用ldply失败的简单方法,因为它尝试将每个列表元素转换为数据帧,但是因为长度不匹配而使用NULL barfs的方法.我现在最好的是:
> ldply(alist,function(s){t(data.frame(unlist(s)))})
name age
1 Foo 22
2 Bar <NA>
3 Baz <NA>
Run Code Online (Sandbox Code Playgroud)
但这很icky,数字变量成为一个因素......
注意:此问题的标题已经过编辑,使其成为plyr功能掩盖其dplyr对应项时的问题的规范问题.问题的其余部分保持不变.
假设我有以下数据:
dfx <- data.frame(
group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
sex = sample(c("M", "F"), size = 29, replace = TRUE),
age = runif(n = 29, min = 18, max = 54)
)
Run Code Online (Sandbox Code Playgroud)
有了旧的,plyr我可以使用以下代码创建一个总结我的数据的小表:
require(plyr)
ddply(dfx, .(group, sex), summarize,
mean = round(mean(age), 2),
sd = round(sd(age), 2))
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
group sex mean sd
1 A F 49.68 5.68
2 A M 32.21 6.27
3 B F 31.87 9.80
4 B M 37.54 …Run Code Online (Sandbox Code Playgroud) 作为指导,我更喜欢使用lapply或*ply(来自plyr)在列表元素上应用函数,而不是显式迭代它们.但是,当我必须一次处理一个列表时,这很有效.当函数接受多个参数时,我通常会进行一个循环.
我想知道是否有可能有一个更清洁的结构,仍然是功能性的.一种可能的方法是定义一个类似于Python的函数zip(x,y),它接受输入列表,并返回一个列表,其第i个元素是list(x,y),然后将该函数应用于这个清单.但我的问题是我是否使用最干净的方法.我并不担心性能优化,而是清晰/优雅.
以下是天真的例子.
A <- as.list(0:9)
B <- as.list(0:9)
f <- function(x, y) x^2+y
OUT <- list()
for (n in 1:10) OUT[[n]] <- f(A[[n]], B[[n]])
OUT
[[1]]
[1] 0
[[2]]
[1] 2
...
Run Code Online (Sandbox Code Playgroud)
这是压缩的示例(可以扩展到任意参数):
zip <- function(x, y){
stopifnot(length(x)==length(y))
z <- list()
for (i in seq_along(x)){
z[[i]] <- list(x[[i]], y[[i]])
}
z
}
E <- zip(A, B)
lapply(E, function(x) f(x[[1]], x[[2]]))
[[1]]
[1] 0
[[2]]
[1] 2
...
Run Code Online (Sandbox Code Playgroud) 我有2个数据帧w/5列和每行100行.
id price1 price2 price3 price4 price5
1 11.22 25.33 66.47 53.76 77.42
2 33.56 33.77 44.77 34.55 57.42
...
Run Code Online (Sandbox Code Playgroud)
我想基本上得到相应行的相关性
for(i in 1:100){
cor(df1[i, 1:5], df2[i, 1:5])
}
Run Code Online (Sandbox Code Playgroud)
但没有使用for循环.我假设有一些plyr用来做它但似乎无法做到正确.有什么建议?
R Plyr包中变量(即"变量")之前点的目的是什么?
例如,从R帮助文件:
ddply(.data, .variables, .fun = NULL, ...,
.progress = "none", .drop = TRUE, .parallel = FALSE)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
我有这样的数据集:
df = data.frame(group = c(rep('A',4), rep('B',3)),
subgroup = c('a', 'b', 'c', 'd', 'a', 'b', 'c'),
value = c(1,4,2,1,1,2,3))
group | subgroup | value
------------------------
A | a | 1
A | b | 4
A | c | 2
A | d | 1
B | a | 1
B | b | 2
B | c | 3
Run Code Online (Sandbox Code Playgroud)
我想要的是获得每个组中每个子组的值的百分比,即输出应该是:
group | subgroup | percent
------------------------
A | a | 0.125
A | b | 0.500
A | c …Run Code Online (Sandbox Code Playgroud) 我无法在Ubuntu上的R 3.0.2中安装ggplot2.
我跑的时候
install.packages('ggplot2',dependencies = TRUE)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
> install.packages('ggplot2',dependencies = TRUE)
Installing package into ‘/home/gowthamn/R/x86_64-pc-linux-gnu-library/3.0’
(as ‘lib’ is unspecified)
Warning in install.packages :
dependencies ‘plyr’, ‘testthat’ are not available
also installing the dependencies ‘reshape2’, ‘scales’, ‘Hmisc’
trying URL 'http://cran.rstudio.com/src/contrib/reshape2_1.4.1.tar.gz'
Content type 'application/x-gzip' length 34693 bytes (33 Kb)
opened URL
==================================================
downloaded 33 Kb
trying URL 'http://cran.rstudio.com/src/contrib/scales_0.2.4.tar.gz'
Content type 'application/x-gzip' length 40093 bytes (39 Kb)
opened URL
==================================================
downloaded 39 Kb
trying URL 'http://cran.rstudio.com/src/contrib/Hmisc_3.16-0.tar.gz'
Content type 'application/x-gzip' length 629536 bytes (614 …Run Code Online (Sandbox Code Playgroud) 就像是
sliding = function(df, n, f)
ldply(1:(nrow(df) - n + 1), function(k)
f(df[k:(k + n - 1), ])
)
Run Code Online (Sandbox Code Playgroud)
就像那样使用
> df
n a
1 1 0.8021891
2 2 0.9446330
...
> sliding(df, 2, function(df) with(df,
+ data.frame(n = n[1], a = a[1], b = sum(n - a))
+ ))
n a b
1 1 0.8021891 1.253178
...
Run Code Online (Sandbox Code Playgroud)
直接内部ddply,以便我可以得到它附带的漂亮的语法糖?