我喜欢plyr语法.任何时候我必须使用*apply()命令之一,我最终踢狗并进行为期3天的弯曲.因此,为了我的狗和我的肝脏,在数据帧的每一行上执行ddply操作的简洁语法是什么?
这是一个适用于简单案例的例子:
x <- rnorm(10)
y <- rnorm(10)
df <- data.frame(x,y)
ddply(df,names(df) ,function(df) max(df$x,df$y))
Run Code Online (Sandbox Code Playgroud)
这很好,给了我想要的东西.但是如果事情变得更复杂,这会导致plyr变得时髦(并且不像Bootsy Collins)因为plyr正在咀嚼从所有那些浮点数值中取出"等级"
x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000)
myLetters <- sample(letters, 1000, replace=T)
df <- data.frame(x,y, z, myLetters)
ddply(df,names(df) ,function(df) max(df$x,df$y))
Run Code Online (Sandbox Code Playgroud)
在我的盒子上咀嚼几分钟,然后返回:
Error: memory exhausted (limit reached?)
In addition: Warning messages:
1: In paste(rep(l, each = ll), rep(lvs, length(l)), sep = sep) :
Reached total allocation of 1535Mb: see help(memory.size)
2: In paste(rep(l, each = ll), rep(lvs, length(l)), sep = sep) :
Reached total allocation of 1535Mb: see help(memory.size)
Run Code Online (Sandbox Code Playgroud)
我认为我完全滥用plyr而我并不是说这是plyr中的一个错误,而是我的滥用行为(尽管肝脏和狗).
那么简而言之,是否有使用ddply操作每行作为替代的语法快捷方式apply(X, 1, ...)?
我一直在使用的解决方法是创建一个"键",为每一行提供一个唯一的值,然后我可以加入它.
x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000)
myLetters <- sample(letters, 1000, replace=T)
df <- data.frame(x,y, z, myLetters)
#make the key
df$myKey <- 1:nrow(df)
myOut <- merge(df, ddply(df,"myKey" ,function(df) max(df$x,df$y)))
#knock out the key
myOut$myKey <- NULL
Run Code Online (Sandbox Code Playgroud)
但我一直在想"必须有一个更好的方式"
谢谢!
had*_*ley 43
只需将其视为数组并处理每一行:
adply(df, 1, transform, max = max(x, y))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11013 次 |
| 最近记录: |