- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 ave
在数据框上的许多列(十)上运行:
ave(df[,the_cols], df[,c('site', 'month')], FUN = mean)
ave
运行
mean
全部功能
the_cols
列在一起。有什么方法可以为每个
the_cols
运行它吗?分别列?
tapply
和
aggregate
不同,它们每组只返回一行。我需要
ave
行为,即返回与原始
df
中给定的行数相同的行数.还有一个
by
函数,但是使用它会非常笨拙,因为它返回一个复杂的列表结构,必须以某种方式进行转换。
最佳答案
也许我遗漏了一些东西,但是 apply()
这里的方法效果很好,不会丑陋或需要任何丑陋的黑客。一些虚拟数据:
df <- data.frame(A = rnorm(20), B = rnorm(20), site = gl(5,4), month = gl(10, 2))
sapply(df[, c("A","B")], ave, df$site, df$month)
data.frame()
将其强制为数据框如果你真的想要那个。
R> sapply(df[, c("A","B")], ave, df$site, df$month)
A B
[1,] 0.0775 0.04845
[2,] 0.0775 0.04845
[3,] -1.5563 0.43443
[4,] -1.5563 0.43443
[5,] 0.7193 0.01151
[6,] 0.7193 0.01151
[7,] -0.9243 -0.28483
[8,] -0.9243 -0.28483
[9,] 0.3316 0.14473
[10,] 0.3316 0.14473
[11,] -0.2539 0.20384
[12,] -0.2539 0.20384
[13,] 0.5558 -0.37239
[14,] 0.5558 -0.37239
[15,] 0.1976 -0.22693
[16,] 0.1976 -0.22693
[17,] 0.2031 1.11041
[18,] 0.2031 1.11041
[19,] 0.3229 -0.53818
[20,] 0.3229 -0.53818
AVE <- function(df, cols, ...) {
dots <- list(...)
out <- sapply(df[, cols], ave, ...)
out <- data.frame(as.data.frame(dots), out)
names(out) <- c(paste0("Fac", seq_along(dots)), cols)
out
}
R> AVE(df, c("A","B"), df$site, df$month)
Fac1 Fac2 A B
1 1 1 0.0775 0.04845
2 1 1 0.0775 0.04845
3 1 2 -1.5563 0.43443
4 1 2 -1.5563 0.43443
5 2 3 0.7193 0.01151
6 2 3 0.7193 0.01151
7 2 4 -0.9243 -0.28483
8 2 4 -0.9243 -0.28483
9 3 5 0.3316 0.14473
10 3 5 0.3316 0.14473
11 3 6 -0.2539 0.20384
12 3 6 -0.2539 0.20384
13 4 7 0.5558 -0.37239
14 4 7 0.5558 -0.37239
15 4 8 0.1976 -0.22693
16 4 8 0.1976 -0.22693
17 5 9 0.2031 1.11041
18 5 9 0.2031 1.11041
19 5 10 0.3229 -0.53818
20 5 10 0.3229 -0.53818
...
的详细信息暂时避开我,但您应该能够为
Fac1
获得更好的名称等我在这里使用。
aggregate()
但使用
ave()
函数而不是
mean()
:
R> aggregate(cbind(A, B) ~ site + month, data = df, ave)
site month A.1 A.2 B.1 B.2
1 1 1 0.0775 0.0775 0.04845 0.04845
2 1 2 -1.5563 -1.5563 0.43443 0.43443
3 2 3 0.7193 0.7193 0.01151 0.01151
4 2 4 -0.9243 -0.9243 -0.28483 -0.28483
5 3 5 0.3316 0.3316 0.14473 0.14473
6 3 6 -0.2539 -0.2539 0.20384 0.20384
7 4 7 0.5558 0.5558 -0.37239 -0.37239
8 4 8 0.1976 0.1976 -0.22693 -0.22693
9 5 9 0.2031 0.2031 1.11041 1.11041
10 5 10 0.3229 0.3229 -0.53818 -0.53818
关于按列狂欢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21340431/
这是一个 bash 脚本,它打印视频的帧率,它所采用的参数可能包含空格。这个参数也将在脚本内的命令中使用。 #!/bin/bash inputVid="$*" #checking if $inputV
OSX Snow Leopard 上的 Bash 脚本,我想删除一组文件后缀中的任何一个,但我的 googlefu 失败了。 给定 file="filename_1" file1=${file%_1}
您好,正在尝试按月和日对列表进行排序。弄不直。有什么指点吗? 列表示例: Jul 23 Drive :NTFS (Windows 31.6 GB/29.4 GiB) Details : Mounted
我正在写一个 bash 脚本 我的文件是这样的: file="${nodeID}_00000_19700101010${ts}_udp_filtered.pcap"。是否可以使用任何 5 位数字代替
我正在尝试通过 bash 代码将文件名传输到 eog。 这是我的代码:ls | grep“ sample ” | head -1 其中 sample 是所需文件的名称。 我无法弄清楚如何将输出通过管道
我是 bash 的新手,我正在尝试编写一个 bash 脚本来保存用户的输入(一些标签),以帮助这个用户我想允许仅根据我个人的预定义标签列表完成而不是关于常见的TAB文件补全(文件夹中的文件名)。 pr
我有一个看起来像这样的字符串/foo/bar/baz59_ 5stuff.thing 如果最后一个数字(示例中的 5)大于另一个变量,我想将其增加一个。 9 将增加到 10。请注意,最后一个数字也可以
我可以用任何其他语言来做到这一点,但是对于 Bash,我四处寻找,却找不到答案。 我需要在脚本中手动增加 $line。示例: for line in `cat file` do foo()
我正在尝试剪切一个字符串,直到它成为第一个指定的字符。在这种情况下,它是第一个拉丁字母。 我厌倦了这个。它有点管用,但有时会移动 1 个或多个字符。 f=$(echo $f | tail -c $((
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
我在名为 file.txt 的文件中有类似的内容 AA.201610.pancake.Paul AA.201610.hello.Robert A.201610.hello.Mark 现在,我只获得
我有这个脚本可以生成 8-16 之间的随机字符。我对如何从银行 [! @ # $ % ^ & * ( ) _ + ] 这个字符串中的任意位置? if [ $# -eq 0 ] then
我有以下 bash 脚本: 1 #!/bin/bash 2 query='query= SELECT * WHERE { ?s ?p ?o } LIMIT 5' 3 cmd="curl $
我正在尝试创建一个安装后 bash 脚本,我可以在全新安装 Ubuntu 时使用它。我想要的第一个功能是让脚本提示用户他们是否想更新他们的系统,如果他们选择是,则显示单词“正在加载”,后面跟着一个点循
我尝试使用 bash 脚本访问特定路径,但我的终端无法访问它。这是我非常简单的脚本: #!/bin/bash init() { cd $PATH ls -l } PATH="/Volu
以下命令通过递归搜索当前目录和所有子目录找到所有出现的“some string” grep -r -n 'some string' . 此命令递归搜索当前目录和所有子目录并返回 *.axvw 格式的
我想让我的一部分代码更有效率。我正在考虑让它分成多个进程并让它们一次执行 50/100 次,而不是一次。 例如(伪): for line in file; do foo; foo2; foo3; do
假设我有字符串“Memory Used: 19.54M”我如何从中提取 19.54?19.54 会经常变化,所以我需要将它存储在一个变量中,并与下一次迭代的值进行比较。 我想我需要 grep 和 re
我有一个简单的 bash 脚本如下 #!/bin/bash sleep 5000 当我尝试执行它时,出现了一个奇怪的错误 : No such file or directory 注意分号:。所有常见的
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我是一名优秀的程序员,十分优秀!