我正在尝试在数据框的两列之间对每一行应用简单的线性回归。经过一番研究,我觉得我已经差不多了,但我的功能仍然不起作用。请看一下:
set.seed(1)
DF <- data.frame(A=rnorm(50, 100, 3),
B=rnorm(50, 100, 3))
resultlist <- apply(DF, 1, function(y) lm(y ~ x))
resultcoeffs <- apply(DF, 1, function(y) lm(y ~ x)$coefficients)
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一目标有什么建议吗?
提前致谢。
我正在尝试手动更改 ggplot2 线图中图例的第一项的颜色。
我对我在折线图中显示的变量有几个观察结果,就像这样:
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) +
geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
xlab(NULL) + ylab('%') + theme(legend.title = element_blank()) + theme(axis.text.x=element_blank()) +
ggtitle("a) Cloud fraction") + theme(plot.title = element_text(hjust = 0))
Run Code Online (Sandbox Code Playgroud)
但是,第一个变量 ( CRU) 是我的参考,我想用黑色显示它的线条。我设法通过添加一个额外的 geom_line 条件来做到这一点variable=='CRU':
ggplot(tmp1, aes(x=factor(month), y=value, group=variable, colour=variable ) ) +
geom_line(size=1) + geom_point(size=2.5) + theme_grey(base_size = 18) +
geom_line(data=subset(tmp1, variable == "CRU"), colour="black", linetype="solid", size=1) +
geom_point(data=subset(tmp1, variable == "CRU"), colour="black", size=2.5) +
xlab(NULL) …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中包含多个地点的每月温度数据:
> df4[1:36,]
location variable cut month year freq
1 Adamantina temperature 10 Jan 1981 21.0
646 Adamantina temperature 10 Feb 1981 20.5
1291 Adamantina temperature 10 Mar 1981 21.5
1936 Adamantina temperature 10 Apr 1981 21.5
2581 Adamantina temperature 10 May 1981 24.0
3226 Adamantina temperature 10 Jun 1981 21.5
3871 Adamantina temperature 10 Jul 1981 22.5
4516 Adamantina temperature 10 Aug 1981 23.5
5161 Adamantina temperature 10 Sep 1981 19.5
5806 Adamantina temperature 10 Oct 1981 21.5
6451 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ggplot2 在 R 中的条形图上绘制误差条。条形的位置没问题,但是误差条未对齐、重叠。请看下面的例子:
library(ggplot2)
df = structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L), .Label = c("Stage1", "Stage2", "Stage3", "Stage4"), class = "factor"),
scenario = c("A", "A", "A", "B", "B",
"B", "A", "A", "A", "B", "B",
"B", "A", "A", "A", "B", "B",
"B", "A", "A", "A", "B", "B",
"B"), Period = c("2011-2040", "2041-2070", "2071-2100",
"2011-2040", "2041-2070", "2071-2100", "2011-2040", "2041-2070",
"2071-2100", …Run Code Online (Sandbox Code Playgroud) 我正在 ggplot 上绘制以下数据:
library(ggplot2)
DF <- structure(list(Type = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("Observed", "Simulated"), class = "factor"),
variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("EM to V6",
"V6 to R0", "R0 to R4", "R4 to R9"), class = "factor"), value = c(28,
30, 29, 35, 32, 34, 26, 29)), row.names = c(NA, -8L), .Names = c("Type",
"variable", "value"), class = "data.frame")
ggplot(DF, aes(variable, value)) +
geom_bar(aes(fill = Type), position …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据框:
dat <- structure(list(Geocode = c("1100015", "1100023", "1100031", "1100049",
"1100056", "1100064", "1100072", "1100080", "1100098", "1100106",
"1100114", "1100122", "1100130", "1100148", "1100155", "1100189",
"1100205", "1100254", "1100262", "1100288", "1100296", "1100304",
"1100320", "1100338", "1100346", "1100379", "1100403", "1100452",
"1100502", "1100601"), Location = c("Alta Floresta D'oeste, RO",
"Ariquemes, RO", "Cabixi, RO", "Cacoal, RO", "Cerejeiras, RO",
"Colorado Do Oeste, RO", "Corumbiara, RO", "Costa Marques, RO",
"Espigo D'oeste, RO", "Guajar-Mirim, RO", "Jaru, RO", "Ji-Paran, RO",
"Machadinho D'oeste, RO", "Nova Brasilndia D'oeste, RO", "Ouro Preto Do Oeste, RO",
"Pimenta …Run Code Online (Sandbox Code Playgroud) 我有以下矩阵:
m <- matrix(1:9, ncol=3, byrow=TRUE)
m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Run Code Online (Sandbox Code Playgroud)
我需要展平,即转换为向量。
然而,不要沿着列:
as.vector(m)
[1] 7 4 1 8 5 2 9 6 3
Run Code Online (Sandbox Code Playgroud)
我需要得到的向量沿着行从底部到右侧,例如:
[1] 7 8 9 4 5 6 1 2 3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在处理一个具有维度的数组
[1] 290 259 55 4
Run Code Online (Sandbox Code Playgroud)
对于最后三个维度的每次重复,我想对第一个维度的 290 个元素执行滚动平均值,将元素数量减少到 289 个。最后,我需要使用更新后的值创建一个数据框。
下面的代码实现了我所需要的,但是需要很长的时间才能运行(实际上,我必须在结束之前中断它)。
library(zoo)
# Generate random data with same dimensions as mine
my.array <- array(1:16524200, dim=c(290,259,55,4))
# Get dimension sizes
dim2 <- dim(my.array)[2]
dim3 <- dim(my.array)[3]
dim4 <- dim(my.array)[4]
# Pre-allocate data frame to be used within the loop
df2 <- data.frame()
# Loop over dimensions
for (i in 1:dim4) {
for (j in 1:dim3) {
for (k in 1:dim2) {
# Take rolling average
u <- rollapply(my.array[,k,j,i], 2, mean) …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的数字向量列表:
lst45 <- structure(list(CanESM2 = structure(c(75172.3232265472, 68514.4574398041, 70116.8608039856, 74918.7828659058, 74355.116765213, 70350.6421344757, 67336.5649261475, 71819.5331493378, 71239.2236221314), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), `GFDL-ESM2M` = structure(c(74736.6871696472, 74668.9771896362, 70693.9238479614, 72538.5751586914, 68865.4270866394, 76652.1024021149, 72507.3696491241, 72044.253433609, 70758.3188240051), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), inmcm4 = structure(c(64797.6587924957, 70213.8876274109, 72865.955796051, 72756.7691856384, 76886.3437725067, 78064.3871330261, 70558.1268108368, 71354.149344635, 72268.3041442871), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), `MRI-CGCM3` = structure(c(83597.9063896179, 79029.3697322845, 83841.0448295593, 82892.550799942, 75588.5374900818, 74541.5410072327, 84481.3175567627, 74491.1396362305, 79476.2405437469), …Run Code Online (Sandbox Code Playgroud) 我有两个data.tables显示多个位置的温度(由地理编码标识).
我想基于它们的减法创建第三个.他们来了:
library(data.table)
# Generate random data:
geocode <- paste0("N", 1:10)
dates <- seq(as.Date("2000-01-01"), as.Date("2004-12-31"), by="month")
models <- c("A", "B", "C", "D", "E")
temp <- runif(length(geocode)*length(dates)*length(models), min=0, max=30)
dt1 <- data.table(expand.grid(Location=geocode,Date=dates,Model=models),Temperature=temp)
ref <- runif(length(geocode), min=0, max=30)
dt2 <- data.table(expand.grid(Location=geocode), Temperature=ref)
Run Code Online (Sandbox Code Playgroud)
我想从dt1中有条件地减去dt2.通过每个位置(地理编码),我想从dt1中的温度中减去dt2中的温度,保留其他列(Date和Model).
怎么做到这一点?我知道如果它是一个单一的数据表怎么做,但我从来没有试过在这样的两个不同的数据表上做代数.
我有一些以农村财产命名的文件,如下所示:
v1 <- c("Badger", "Hill", "Farm", "1.json")
v2 <- c("Buffalo", "Pass", "Farm", "2.json")
> v1
[1] "Badger" "Hill" "Farm" "1.json"
> v2
[1] "Buffalo" "Pass" "Farm" "2.json"
Run Code Online (Sandbox Code Playgroud)
我设法分割文件名元素,但我只想保留那些不包含任何数字的元素。期望的输出是:
> v1
[1] "Badger" "Hill" "Farm"
> v2
[1] "Buffalo" "Pass" "Farm"
Run Code Online (Sandbox Code Playgroud)
很简单,但我就是无法理解它。我怎样才能做到这一点?
我用来ggplot2创建以下情节:
library(ggplot2)
df <- data.frame(site=rep(c("CA-Oas","US-Oho","CA-Obs","CA-TP1"), each=124),
exper=rep(c("bberry","medlyn"), each=62, 4),
sm=runif(496, min=0, max=100),
lh=runif(496, min=0, max=400),
month=rep(c("Jul","Aug"), each=31, 8))
ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) +
geom_point(size=2.5, alpha=0.9) +
facet_wrap(~site, nrow=2) +
theme_bw(base_size=18) +
ylab(bquote('Latent heat ('*W~m^-2*')')) +
xlab(bquote('Soil water content (%)')) +
scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
legend.title=element_blank(),
legend.box="horizontal",
legend.position=c(0.4, 0.6),
legend.spacing = unit(-0.2,"cm"))
Run Code Online (Sandbox Code Playgroud)
然而,我正在写手稿的期刊要求所有图在两个轴(上/下和左/右)上都有刻度。我正在努力实现这一目标ggplot2。
基本上,我需要的是:
关于如何到达那里有任何提示吗?
我正在处理从以下输出创建的矩阵列表combn:lapply(2:length(c("A","B","C","D","E")), function(n) combn(c("A","B","C","D","E"), n, simplify = TRUE))
它看起来像这样:
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"
[2,] "B" "C" "D" "E" "C" "D" "E" "D" "E" "E"
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "A" "A" "B" "B" "B" "C"
[2,] "B" "B" "B" "C" "C" "D" "C" "C" "D" "D"
[3,] "C" "D" …Run Code Online (Sandbox Code Playgroud)