我想对数据集中的列对进行平均,而不是使用移动平均线。我想将列分成两组,并找到每对的平均值。
我提供了一个示例数据集、所需的结果以及返回所需结果的嵌套 for 循环。我只是认为可能有更好的方法。抱歉,如果我忽略了另一篇文章中的解决方案。我确实在这里搜索过,但我没有像平时那样勤奋地搜索互联网。感谢您的任何建议。
x = read.table(text = "
site yr1 yr2 yr3 yr4
1 2 4 6 8
2 10 20 30 40
3 5 NA 2 3
4 100 100 NA NA",
sep = "", header = TRUE)
x
desired.outcome = read.table(text = "
site ave12 ave34
1 3 7
2 15 35
3 5 2.5
4 100 NA",
sep = "", header = TRUE)
result <- matrix(NA, ncol=((ncol(x)/2)+1), nrow=nrow(x))
for(i in 1: ((ncol(x)-1)/2)) {
for(j in 1:nrow(x)) …Run Code Online (Sandbox Code Playgroud) 我有一个向量“nameAlpha”,例如c(“Mark Twain”,“Phil Hall”,“Michael P. O'Connor”,“”,...)。我想将每个名字传递给另一个向量“nameAlpha_first”。我运行这个
nameAlpha_first <- sapply(strsplit(nameAlpha, "\\s+"), "[[", 1)
Run Code Online (Sandbox Code Playgroud)
但我得到
Error in FUN(X[[12L]], ...) : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)
难道是因为向量中很少有元素是空的吗?我该如何解决它?
我在 R 中读取了一个包含 n 列的数据框 (df<-read.csv(data,as.is=T)),其中一个是像这样的 char 列:
df$qual
===========
1/5
12/17
...
0/3
9/14
Run Code Online (Sandbox Code Playgroud)
我想将此列转换为数字向量,仅保留每行的第一个元素。
df$qual
===========
1
12
...
0
9
Run Code Online (Sandbox Code Playgroud)
我想有更简单的方法(欢迎提出想法!),但我尝试了 sapply:
sapply(df$qual,strsplit() ,simplify=T)
Run Code Online (Sandbox Code Playgroud)
现在的问题是我如何/在哪里传递参数 split="/" 以便它起作用?R帮助并没有多大帮助。
提前致谢,第
我有一个数据集,我通过列绑定使用包中的cbindX函数创建gdata.此函数允许我绑定具有不同行数的列.因此,NA当特定列中没有值时会引入.现在,我想计算每列的标准偏差.我试过用
sapply(dataset,sd)
Run Code Online (Sandbox Code Playgroud)
这将返回具有值的所有行的列的标准偏差以及NA具有较少行的列的标准偏差.我尝试使用函数的na.rm参数sd:
sapply(dataset,sd(na.rm=T))
Run Code Online (Sandbox Code Playgroud)
并收到错误消息
Error in is.data.frame(x) : argument "x" is missing, with no default
Run Code Online (Sandbox Code Playgroud)
例如:
firstcol <- matrix(c(1:150),ncol=1)
secondcol <- matrix(c(1:300),ncol=1)
thirdcol <- matrix(c(1:450),ncol=1)
fourthcol <- matrix(c(1:600),ncol=1)
fifthcol <- matrix(c(1:30),ncol=1)
sixthcol <- matrix(c(1:30),ncol=1)
seventhcol <- matrix(c(1:30),ncol=1)
library(gdata)
allcolscomb <- data.frame(cbindX (firstcol,secondcol,thirdcol,fourthcol,fifthcol,sixthcol,seventhcol))
names(allcolscomb) <- c("1stcol","2ndcol","3rdcol","4thcol","5thcol","6thcol","7thcol")
sapply(allcolscomb,sd)
sapply(allcolscomb,sd(na.rm=T))
Run Code Online (Sandbox Code Playgroud)
如何使用该sapply函数计算标准偏差?
可能这很容易.我有一个矩阵:
testM <- matrix(1:40, ncol = 4, byrow = FALSE)
testM
[,1] [,2] [,3] [,4]
[1,] 1 11 21 31
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39
[10,] 10 20 30 40
Run Code Online (Sandbox Code Playgroud)
我希望逐行"减少"矩阵求和列.预期结果:
[,1] [,2]
[1,] 12 52
[2,] 14 54
[3,] 16 56
[4,] 18 58 …Run Code Online (Sandbox Code Playgroud) 我正在单个数据框中跨多个组运行简单的单向方差分析。
\n\n此处提供数据框:https ://www.dropbox.com/s/6nsjk4l1pgiwal3/cut1.csv?dl=0
\n\n>download.file(\'https://www.dropbox.com/s/6nsjk4l1pgiwal3/cut1.csv?raw=1\', destfile = "cut1.csv", method = "auto")\n\n> data <- read.csv("cut1.csv")\n> cut1 <- data %>% mutate(Plot = as.factor(Plot), Block = as.factor(Block), Cut = as.factor(Cut)) \n\n> str(cut1)\n\'data.frame\': 160 obs. of 6 variables:\n $ Plot : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...\n $ Block : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 2 2 2 2 3 3 ...\n $ Treatment : Factor w/ 4 …Run Code Online (Sandbox Code Playgroud) 我有一个 .rda (RData) 文件列表。我想快速将此数据加载到 R 中,而不必load多次调用该函数。我想到将load()函数与sapply. 但是,使用以下代码,不会在工作区中加载任何 R 对象:
# List files
gewataPath <- list.files(path = file.path(datdir), pattern = glob2rx('Gewata*.rda'), full.names = T)
# Load files
sapply(gewataPath, function(file) {load(file)})
Run Code Online (Sandbox Code Playgroud)
它也不会给任何错误。
运行循环确实会将 .rda 文件作为 RasterLayer 对象加载到 R 工作区中:
for (i in 1:length(gewataPath)) {
load(gewataPath[i])
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我不能使用apply()函数将 .rda 文件快速加载到 R 工作区中,我是否必须使用循环?
关于数据:数据包含位于埃塞俄比亚 Gewata 的 RasterLayers(来自 Landsat 卫星)。
我正在尝试使用 (s)apply 将两个不同维度的矩阵相乘。矩阵是:
xx <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3, byrow=T)
yy <- matrix(c(10, 100), nrow=2, ncol=1, byrow=T)
Run Code Online (Sandbox Code Playgroud)
我想要的是将一个矩阵的每一行与另一个矩阵的每一行相乘并获得:
> zz
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 400 500 600
Run Code Online (Sandbox Code Playgroud)
我试过sapply(yy, function(x) xx*x)产生一个 6x2 矩阵而不是我想要的 3x2 矩阵。还apply(yy, 2, function(x) xx*x)产生一个6X1矩阵不起作用。在过去的类似情况下,我使用sapply没有问题,所以我不明白为什么这现在不起作用(我总是在绕着头走时遇到一些麻烦*apply)。我究竟做错了什么?
例如,对于向量的每个元素,我想计算残差与该向量的其他元素的总和。这适用于一个元素:
a = [1, 2, 5, 7, 8, 22]
f(x) = sum(abs.(x .- a))
f(2)
Out: 35
Run Code Online (Sandbox Code Playgroud)
但是如果使用 map() 将此函数应用于所有元素,Julia 将返回错误:
map(a, f)
Out: "MethodError: no method matching iterate(::typeof(f))"
Run Code Online (Sandbox Code Playgroud)
在 R 中,使用 sapply() 很容易获得:
a = c(1, 2, 5, 7, 8, 22)
sapply(a, function(x) sum(abs(x - a)))
Out: 39 35 29 29 31 87
Run Code Online (Sandbox Code Playgroud)
在 Julia 中是否有同样优雅的方法来做到这一点?
我想将具有多个参数的函数应用于向量。看来 和 都有map()帮助map!()。
如果函数有一个参数,它就可以完美地工作:
f = function(a)
a+a
end
x=[1,2,3,4,5]
map(f, x)
Run Code Online (Sandbox Code Playgroud)
输出:[2, 4, 6, 8, 10]
但是,尚不清楚如何将参数传递给函数(如果可能)以及要广播的向量(如果函数有多个参数)。
f = function(a,b)
a*b
end
Run Code Online (Sandbox Code Playgroud)
但是,以下功能均不起作用:
b=3
map(f(a,b), x, 3)
map(f, x, 3)
map(f, a=x, b=3)
map(f(a,b), x, 3)
map(f(a,b), a=x,b=3)
Run Code Online (Sandbox Code Playgroud)
预期输出:
[3,6,9,12,15]