如果有人可以帮助我,我将非常感激.这看起来很简单,但我不知道如何去做.
我试图计算出相对于前一行的百分比变化.例如:我的数据框如下所示:
day value
1 21
2 23.4
3 10.7
4 5.6
5 3.2
6 35.2
7 12.9
8 67.8
. .
. .
. .
365 27.2
Run Code Online (Sandbox Code Playgroud)
我想要做的是计算每行相对于前一行的百分比变化.例如:
day value
1 21
2 (day2-day1/day1)*100
3 (day3-day2/day2)*100
4 (day4-day3/day3)*100
5 (day5-day4/day4)*100
6 (day6-day5/day5)*100
7 (day7-day6/day6)*100
8 (day8-day7/day7)*100
. .
. .
. .
365 (day365-day364/day364)*100
Run Code Online (Sandbox Code Playgroud)
然后打印出那些从前一行增加百分比> 50%的日子
非常感谢
我有10个光栅文件.我想要做的是这样的:
1)读取R中的第一个栅格(光栅文件)
2)将该文件保存在文件夹中(在循环中创建文件夹)
3)再次读取第二个光栅文件
4)将该文件保存在新文件夹中(也在循环中创建)
5)不断重复10次
这是我设法做的:
for (i in 1:10){
dir.create(paste0("Run",i)) #this creates a new folder called Run[i] where I will save the raster
setwd(paste0("Run",i)) # this makes the Run[i] my working directory so that my first raster is saved in Run[i]
moist<-raster(paste0("R://moist_tif/ind_moist",i,".tif")) # this reads in my raster moist[i]
writeRaster(moist,"moist.tif") # this saves my raster in folder Run[i]
Run Code Online (Sandbox Code Playgroud)
正如您可能注意到循环移动到的那样i+1,Run[i+1]创建了Run[i]我不想要的新文件夹.我想为文件夹中的文件夹Run[i+1]而不是文件夹创建单独的文件夹.希望我能清楚地写出这个问题.谢谢您的帮助.
问候
我最近开始在 R 中使用 netcdf。示例数据在这里:
http://www.earthstat.org/data-download/ > 175 种作物的收获面积和产量 > 个别作物 > 大豆_HarvAreaYield2000_NetCDF
在这个文件夹中,有一个名为的 netcdf 文件 soybean_AreaYieldProduction.nc
这就是我打开 netcdf 的方式
library(ncdf4)
dat <- nc_open("soybean_AreaYieldProduction.nc")
print(soy)
1 variables (excluding dimension variables):
float soybeanData[longitude,latitude,level,time]
LayerDescriptions: struct(5).Data(:,:,1/2/3/4/5/6) to access data layer: 1=Harvested Area fraction, 2=Yield 3=Harvested Area data quality, 4=Yield data quality, 5=Harvested Area in hectares, 6= Production
Units: Harvested Area Fraction(1)=percent of gridcell that was harvested, Yield(2)=metric tons per hectare, Harvested Area Hectares(5)=total hectares harvested per gridcell, Production(6)=Metric Tons
DataQuality: In levels 3 …Run Code Online (Sandbox Code Playgroud) 我有一个数据,一年有40天和一些数据
set.seed(123)
df <- data.frame(day = 1:40,rain = runif(40,min = 0, max = 3), petc = runif(40, min = 0.3, max = 8),swc = runif(40, min = 27.01, max = 117.43))
Run Code Online (Sandbox Code Playgroud)
我想计算每天另一个名为aetc的变量,计算方法如下:
SW.ini <- 2 # setting some initial values
SW.max <- 5
SW.min <- 0
Run Code Online (Sandbox Code Playgroud)
第1天,
1)确定一个名为的变量 PAW(day1) = SW.ini + rain(day1)
2)如果PAW(day1) >= SWC(day1), aetc(day1) = petc(day1);
If `PAW(day1) < SWC(day1), aetc(day1) = PAW(day1)/SWC(day1) * petc(day1)`
Run Code Online (Sandbox Code Playgroud)
3)检查是否 aetc(day1) > PAW(day1). If yes, aetc(day1) = paw(day1)
4)更新 …
library(raster)
library(ggplot2)
map.shp <- getData('GADM', country='FRA', level=1)
plot(map.shp)
ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
如何map.shp将ggplot的右侧作为小插图放置.
我正在尝试从包装中绘制巴西地图。这是我的方法
install.packages("brazilmaps")
library(brazilmaps)
test <- get_brmap(geo = "State", geo.filter = list(State = 33), class = "SpatialPolygonsDataFrame")
Error: .onLoad failed in loadNamespace() for 'sf', details:
call: get(genname, envir = envir)
error: object 'group_map' not found
Run Code Online (Sandbox Code Playgroud)
我无法理解此错误。我如何解决它?
编辑我想出了问题。我必须重新安装sf和rgal软件包才能正常工作。我以前做过一些有趣的事。
我做了以下事情:
season<-rep(c("aut","win","sum"), each=520)
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我aut重复520次,然后win重复520次,然后sum重复520次.我想这样做的16倍,即aut520,win520,sum520,然后再aut520,win520,sum520,再次...... 16倍.谁能告诉我怎么做?
谢谢
样本数据:
library(tidyverse)
set.seed(123)
dat <- tibble(
year = rep(1980:2015, each = 100),
day = rep(200:299, times = 36),
rain = sample(0:17, size = 100*36,replace = T),
PETc = sample(rnorm(100*36)),
ini.t = rep(10:45, each = 100 ))
Run Code Online (Sandbox Code Playgroud)
我有一个在DataFrame上运行的函数
my.func <- function(df, initial, thres, upper.limit){
df$paw <- rep(NA, nrow(df))
df$aetc <- rep(NA, nrow(df))
df$sw <- rep(NA, nrow(df))
for(n in 1:nrow(df)){
df$paw[n] <- df$rain[n] + initial
df$aetc[n] <- ifelse(df$paw[n] >= thres, df$PETc[n], (df$paw[n]/thres) * df$PETc[n])
df$aetc[n] <- ifelse(df$aetc[n] > df$paw[n], df$paw[n], df$aetc[n])
df$sw[n] …Run Code Online (Sandbox Code Playgroud) 样本数据
dat <- data.frame(year = as.factor(rep(c(2012:2015),each = 6)),id.2wk = rep(c(18,19,20,21,22,23),times = 4),
value = c(1.8,15.6,32.9,27.5,19.6,2.6,1,8,42,35,11,3,2,7,12,47,26,7,2,13,24,46,12,4))
with(dat, plot(id.2wk[year == 2012], cumsum(value[year == 2012]), type = "b"))
with(dat, points(id.2wk[year == 2013], cumsum(value[year == 2013]), type = "b"))
with(dat, points(id.2wk[year == 2014], cumsum(value[year == 2014]), type = "b"))
with(dat, points(id.2wk[year == 2015], cumsum(value[year == 2015]), type = "b"))
Run Code Online (Sandbox Code Playgroud)
我想使用 ggplot2 创建相同的图。我这样做了:
ggplot(dat, aes(x = id.2wk, y = cumsum(value), colour = factor(year))) +
geom_line(size = 1)+
geom_point()
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
x <- seq(1:100)
y <- 30
Run Code Online (Sandbox Code Playgroud)
如果我想确定x> y的累积和的位置
which.max(cumsum(x) >= y)
[1] 9 #
Run Code Online (Sandbox Code Playgroud)
相反,如果y是一个向量,即我想为y的每个元素返回x的位置
y <- c(30, 60, 80, 90)
which.max(cumsum(x) >= y)
[1] 9
Run Code Online (Sandbox Code Playgroud)
为什么不重新调整位置向量?
我想在图例中显示某些类别,即使形状文件中不存在。例如,考虑下面的例子:
library(raster)
library(sp)
library(sf)
library(viridis)
shp <- getData('GADM', country = "FRA", level = 1)
shp_sf <- st_as_sf(shp)
temp_df <-
data.frame(NAME_1 = shp@data$NAME_1,
value = c(1, 15, 28, 80, 32, 90, 29, 12, 43, 44, 27, 2, 6))
temp_df$value_cut <- cut(temp_df$value,
breaks = c(0,11, 21, 31, 41, 51, 61, 71, 81, 91, 100),
labels = c("1-11", "11-21", "21-31", "31-41", "41-51","51-61","61-71","71-81","81-91","91-100"),
include.lowest = TRUE)
shp_sf_join <- sp::merge(shp_sf, temp_df, "NAME_1")
ggplot(shp_sf_join) +
geom_sf(aes(fill = value_cut)) +
coord_sf() +
viridis::scale_fill_viridis(name="", option = "G", discrete=T) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 databricks cli 并调用 databricks configure 这就是我从 cmd 中执行的操作
somepath>databricks configure --token
Databricks Host (should begin with https://): my_https_address
Token: my_token
Run Code Online (Sandbox Code Playgroud)
我想使用 R 调用相同的命令。所以我做了:
tool.control <- c('databricks configure --token'
,'my_https_address'
,'my_token')
shell(tool.control)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error in system(command, as.integer(flag), f, stdout, stderr, timeout) :
character string expected as first argument
Run Code Online (Sandbox Code Playgroud)
我该如何纠正?
编辑:尝试评论中的建议后,我收到此错误:
Databricks Host (should begin with https://): Aborted!
'https:' is not recognized as an internal or external command,
operable program or batch file.
'my_token' is not recognized as an internal or …Run Code Online (Sandbox Code Playgroud)