所以这就是问题:我想在我的R代码中使用for循环来总结不同的列.
举个例子,这里看起来像什么:
all.columns<-c("column4","column5","column6","column7")
for (i in 1:4) {
df%>%
group_by(column3)%>%
summarise(Mean=mean(all.columns[i]),
Max=max(all.columns[i]))
}
Run Code Online (Sandbox Code Playgroud)
其中df是数据框,column3可以是Year by Year变量,列5到7是我想用相同代码重复检查的那些.
你知道怎么用dplyr执行这个吗?如果你没有dplyr的替代品,我想听听它.
我试图把列的字符名称,但它不起作用...
我找到了用来计算R中密度曲线下面积之和的代码.不幸的是,我不明白为什么在该区域总有一个额外的"0.000976"......
nb.data = 500000
y = rnorm(nb.data,10,2)
de = density(y)
require(zoo)
sum(diff(de$x[order(de$x)])*rollmean(de$y[order(de$x)],2))
[1] 1.000976
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
它应该等于1,对吧?
我想找到以下解决方案:
-x^3+6*x^2+51*x+44=0
Run Code Online (Sandbox Code Playgroud)
但是有了R.有可能吗?
我找到了Ryacas包,但似乎没有人能够使它工作.
可能听起来微不足道,但我无法找到一个简单的方法来做到这一点......
你有其他选择吗?
多谢你们!
我有一个数据集,我需要计算重新捕获概率.我想如果我在捕获1的两个事件之间求和0(物种不能重新捕获)的值并将该值除以潜在捕获的数量(所有事件1加上1之间的所有事件,所以0)会给我一个重新夺回的概率.
例如,如果我有1 1 0 1这个将是1/4 = .25所以我有25%的机会不重新获得一个物种.在另一种情况下,如果我有,1 0 0 1我会有2/4这样50%的机会重新捕获这个物种.最后,如果我有1 1 1 0,这将是0/3所以0%的物种没有被重新捕获.
有没有办法在此数据集中自动执行此操作?
structure(list(y.2010 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), y.2011 = c(1,
1, 1, 0, 1, 1, 1, 0, 0, 1, 1), y.2012 = c(0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1), y.2013 = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0), y.2014 = c(0,
0, 0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以在5或6个核心的JAGS中细分3个链.这是我的代码:
library(parallel)
# There is no progression bar using parallel
jags.parallel(data = d$data,
inits = d$inits,
parameters.to.save = d$params,
model.file = model.jags,
n.chains = 3,
n.thin = 10,
n.iter = 9000,
n.burnin = 3000,
working.directory = NULL,
n.cluster = 3) ## the number of cluster it’s taking
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是默认值,链的数量(nc在我的情况下为3)等于使用的核心数.
Error in res[[ch]] : subscript out of bounds.为什么?我的计算机有16个内核,所以我在内核数量上有灵活性(还有64 GB的RAM和3 GHz的Intel Xeon E5处理器).
我正在尝试计算两个日期之间低于特定阈值(假设小于或等于 50)的降水量。
基本上,我有一个向量cuts,其中包含我想要计算的日期。我想使用该cuts向量对不同箱中的数据集进行“子集化”,然后计算降雨量小于 50 毫米的事件数量。
我目前正在使用 dplyr 和 for 循环,但没有任何效果。
set.seed(12345)
df = data.frame(date = seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days"),
precipitation = rnorm(length(seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days")),80,20))
cuts = c("2001-11-25","2002-01-01","2002-02-18","2002-03-01")
for (i in 1:length(cuts)) {
df %>% summarise(count.prec = if (date > cuts[i] | date < cuts[i+1]) {count(precipitation <= 50)})
}
Run Code Online (Sandbox Code Playgroud)
但我有这个错误消息:
Error: no applicable method for 'group_by_' applied to an object of class "logical"
In addition: Warning message:
In if (c(11017, 11018, 11019, 11020, 11021, 11022, 11023, 11024, : …Run Code Online (Sandbox Code Playgroud) 我的数据集中有一列,我想要分割一串字符.
df = data.frame(col = c("BrBkRY","BBkRBr","YBRG","RBBk"))
Run Code Online (Sandbox Code Playgroud)
这是我想用来有条件地分割的矢量.
sep = c("Br","Bk","R","Y","B","G")
Run Code Online (Sandbox Code Playgroud)
这应该是最终的样子.我亲手做到了.
df2 = data.frame(col = c("BrBkRY","BBkRBr","YBRG","RBBk"),
col1 = c("Br","B","Y","R"),
col2 = c("Bk","Bk","B","B"),
col3 = c("R","R","R","Bk"),
col4 = c("Y","Br","G",""))
df2
col col1 col2 col3 col4
1 BrBkRY Br Bk R Y
2 BBkRBr B Bk R Br
3 YBRG Y B R G
4 RBBk R B Bk
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用正则表达式,但通常,你需要一个分裂字符,如a .或-.但是根据角色的字符串,我不知道.此外,不想在B,K和B中拆分BkB.但我确实想在Bk和B中将它分开.是否有可以执行此操作的包?
我有 800 多张图片需要提取创建日期。但是如果我使用该file.mtime功能,它就不起作用。这只是我修改它的日期。有趣的是,iPhoto(Mac 上用于管理照片的程序)能够正确检测创建日期。
这是我使用的代码。
my.path = "~/Desktop/cool_path_here"
vec.jpg = list.files(path = my.path)
lapply(paste(my.path,vec.jpg,sep = "/"),FUN = file.mtime)
Run Code Online (Sandbox Code Playgroud)
是否有一种功能可以提取照片的创建日期,而不是 mac 上“获取信息”菜单中显示的日期。
例如:(昨天是 5 月 18 日)
在 iPhoto 中(3 月 16 日发现):
R 发现:
2017-05-19 15:08:29
Run Code Online (Sandbox Code Playgroud) 我想知道它是如何设计美学的ggplot.如何知道在哪里放置aes离子ggplot?
考虑以下代码:
p<- ggplot(mtcars, aes(wt, mpg, colour = "red"))
# A basic scatter plot
hello =p + geom_point(size = 4)
hello
goodbye=p + geom_point(aes(colour = factor(cyl)), size = 4)
goodbye
Run Code Online (Sandbox Code Playgroud)
在这里,可以看到红色应用于第一个图形中的点(hello),而在第二个图标中(再见)它可以从列中获取颜色.但有什么区别?
如果我有一个日期,说"2014-05-13",我想用十进制计算月份,我会这样做:
5 + 13/31 = 5.419355
Run Code Online (Sandbox Code Playgroud)
如何在R中采用日期向量并将其转换为"月十进制"向量?
例如:
dates = c("2010-01-24", "2013-04-08", "2014-03-05", "2013-03-08", "2014-02-14",
"2004-01-28", "2006-02-21", "2013-03-28", "2013-04-01", "2006-02-14",
"2006-01-28", "2014-01-19", "2012-03-12", "2014-01-30", "2005-04-17")
library(lubridate)
month(dates) + day(dates)/31
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,将"31"作为分隔符是错误的,因为天数根据月份而有所不同,有时是年份(闰年).
那么什么是最好的解决方案?
这是一个 DNA 字符串,我想将其拆分,然后以 3 组为一组进行组合
dna=c("TACACGATGACAGTCTTGACGGGTTCTCCTACT")
dna.sg = unlist(strsplit(dna, ""))
Run Code Online (Sandbox Code Playgroud)
给予
[1] "T" "A" "C" "A" "C" "G" "A" "T" "G" "A" "C" "A" "G" "T" "C" "T" "T" "G" "A" "C" "G" "G" "G" "T" "T" "C" "T" "C" "C" "T" "A" "C" "T"
Run Code Online (Sandbox Code Playgroud)
但我想有
"TAC" "ACG" [...]
Run Code Online (Sandbox Code Playgroud) 我想使用facet_wrap 在x 轴上为不同的组绘制不同的值。但是,使用 时scale = "free_x",它会显示每个组内的唯一值。请参阅下面的示例:
library(ggplot2)
set.seed(1234)
nb = rep(LETTERS[1:3],4)
expand.grid(LETTERS[1:3], 1:3)
df = data.frame(ps = paste(rep(c("ps1","gc1"),6)),
cr = rep(LETTERS[1:3],4),
id = rep(x = month.abb[1:4], each = 3));df
df = df[order(df$ps),]
df[df$ps == "ps1","cr"] <- sample(LETTERS[4:6], size = 6, replace = TRUE)
df.all = rbind(df,df,df,df,df,df)
df.all$val = rnorm(n = nrow(df.all))
ggplot(df.all,
aes(x = cr,
y = val)) +
geom_violin() +
facet_wrap(vars(ps, id),
scales = "free_x",
ncol = 4) +
geom_point(position = position_jitter(width = 0.1, height = 0.1), …Run Code Online (Sandbox Code Playgroud) 是否可以在dplyr中管理多个图形.
这是有效的:
birdsss = data.frame(x1 = 1:10,x2 = 21:30,x3 = 41:50)
birdsss%>%
with(hist(x1, breaks = 50))
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
birdsss%>%
with(hist(x1, breaks = 50)) %>%
with(hist(x2, breaks = 50)) %>%
with(hist(x3, breaks = 50))
Error in hist(x2, breaks = 50) : object 'x2' not found
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
birdsss%>%
with(hist(x1, breaks = 50)) &
with(hist(x2, breaks = 50)) &
with(hist(x3, breaks = 50))
Run Code Online (Sandbox Code Playgroud)
和
birdsss%>%
with(hist(x1, breaks = 50)) ;
with(hist(x2, breaks = 50)) ;
with(hist(x3, breaks = 50))
Run Code Online (Sandbox Code Playgroud)
在一行中打印多列的解决方案是什么?
就像是:
birdsss%>%
with(hist(x1:x3, breaks …Run Code Online (Sandbox Code Playgroud)