当然这是可能的,但我似乎无法找到如何做到这一点:
我想要一个函数输入的默认值,但是如果它存在于全局环境中,则覆盖默认值和get()一个来自全局环境的变量.如果它在全局环境中不存在,则取该函数的默认值,函数中的任何设置都是顶级并覆盖它们.
理想情况下,它会像这个非工作函数一样工作:
###Does not work, desired example
myfunc <- function(x=30){
if(exists.in.global.env(x)){x <- get(x)}
###Top level is tough
if(x.is.defined.as.function.input=TRUE ????){x <- x.defined.as.input}
}else{ x <- 30}
return(x)
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
myfunc()
[1] 30
Run Code Online (Sandbox Code Playgroud)
但是,如果我创建x,我希望它覆盖函数的默认x = 30并取代全局环境值:
x <- 100
myfunc()
[1] 100
Run Code Online (Sandbox Code Playgroud)
但是如果我在函数内部定义了x,我希望它是顶级的,即使全局定义x,也要覆盖其他所有内容:
x <- 100
myfunc(x=300)
[1] 300
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正在尝试在linux上安装软件包rgeos.我收到以下错误:
system("sudo apt-get update")
system("sudo apt install libgdal-dev -y")
install.packages("rgeos")
collect2: error: ld returned 1 exit status
configure: Install failure: compilation and/or linkage problems.
configure: error: initGEOS_r not found in libgeos_c.
ERROR: configuration failed for package ‘rgeos’
Run Code Online (Sandbox Code Playgroud)
我已经安装了dev libgdal,如果我再次尝试运行sudo,它会返回"已经安装"的信息.
我怎样才能解决这个问题?提前致谢!
如何在具有多个多边形的 shapefile 中合并多边形要素?
rbind 和 union 只是组合了 shapefile 特征的行,它们实际上并没有合并多边形本身。
以下示例中的所需结果:
如何获取以下带有重复 ID_2 的 shapefile 以合并到 sptemp 中的单个多边形?
下面的埃塞俄比亚 GADM 级别 2 示例具有重复的 shapefile ID_2 列的前两行(值 = 1)。我想要具有 79 个功能的 sptemp,将前两行(即 ID_2 重复的行)组合在一起。sptemp[1,] 的绘图将显示当前 sptemp[1,] 和 sptemp2[2,] 的位置,而没有重复之间的边界,即多边形也合并了。
示例代码:
下载、解压并加载到埃塞俄比亚 2 级的 R GADM 文件(899kb 到工作目录):
library(curl)
library(rgdal)
curl_download("http://biogeo.ucdavis.edu/data/gadm2.8/shp/ETH_adm_shp.zip",
destfile=paste0("gadmETH.zip"),
quiet=FALSE)
unzip(paste0("gadmETH.zip"), exdir="gadmETH", overwrite=FALSE)
###Load shapefile
sptemp <- readOGR(dsn="gadmETH", layer="ETH_adm2")
Run Code Online (Sandbox Code Playgroud)
前两个多边形的 ID_2 列重复
###You'll see in the first two rows ID_2 is duplicated
df.sptemp <- as.data.frame(sptemp)
View(sptemp)
###I can't just delete …Run Code Online (Sandbox Code Playgroud) 我在@jdharrison上找到了关于如何RSelenium在Windows 上启动Tor的精彩答案:https:
//stackoverflow.com/a/39048970/7837376
RSelenium然而,在新版本中,startServer()它已经rsDriver()不存在了,并且它的替换不会像startServer()之前那样采用java参数.
在新的RSelenium语法中,如何在firefox中启动Tor?首先十分感谢)!
我有这样的数据:
example_df <- data.frame(
col1type1 =c(110:106),
col2type2 = c(-108:-104),
col3type1 = c(-109:-105),
col4type2 =c(110:106),
col5type1 =c(107:103),
col6type2 = c(-110:-106),
col7type1 =c(109:113),
col8type2 = c(-120:-116),
col9type1 = c(-105:-101),
col10type2 =c(105:101),
col11type1 = c(-125:-121),
col12type2 = c(-105:-101)
)
Run Code Online (Sandbox Code Playgroud)
我只想返回同一行上 type1+type2>=0 的组合,并返回一个新的 df >=0 的组合、行和两个数字:(我知道我可以使用 for/foreach 来单独计算每个单元格并输出到 data.frame,但必须有更有效的方法)
所需的输出如下(不完整):
#for all possible combinations, like the example rows below
example_first <- data.frame(column_combination="col1type1_col2type2", row=1, sum=2,col1number=110,col2number=-108)
example_mid<- data.frame(column_combination="col1type1_col12type2",row=3, sum=5,col1number=108,col2number=-103)
example_last <- data.frame(column_combination="col9type1_col10type2",row=5,sum=0,col1number=-101,col2number=101)
#would want like this for all possible combinations
desired_incomplete_output <- rbind(example_first,example_mid,example_last)
Run Code Online (Sandbox Code Playgroud)
什么是有效的方法来集体计算而不是残酷的 for/foreach 循环?谢谢!
如何使用 %、- 和 + 作为可能的分隔符将字符列拆分为 3 列,并将分隔符保留在新列中?
示例数据:
data <- data.table(x=c("92.1%+100-200","90.4%-1000+200", "92.8%-200+100", "99.2%-500-200","90.1%+500-200"))
Run Code Online (Sandbox Code Playgroud)
所需数据示例:
data.desired <- data.table(x1=c("92.1%", "90.4%", "92.8%","99.2%","90.1%")
, x2=c("+100","-1000","-200","-500","+500")
, x3=c("-200","+200","+100","-200","-200"))
Run Code Online (Sandbox Code Playgroud)
很高兴为一个好的答案和对这个问题的一些帮助奖励积分!
我有sampledata使用下面的代码复制的数据:
###producing sample data
sampledatain1 <- data.frame(in_1=c(1,2,3,4,5),in_2=c("A","B","C","D","E"),stringsAsFactors = F)
sampledatain2 <- data.frame(in_1=c(6,7,8,9,10),in_2=c("F","G","H","I","J"),stringsAsFactors = F)
sampledatain3 <- data.frame(in_1=c(99,98,97,96,95),in_2=c("Z","Y","X","W","V"),stringsAsFactors = F)
sampledata <- data.frame(row = 1:3,
colmerge1 = c("MA","MB","MC"),
colmerge2 = -99:-97,
stringsAsFactors = FALSE)
sampledata$df <- list(sampledatain1,sampledatain2,sampledatain3)
Run Code Online (Sandbox Code Playgroud)
我想展平所有嵌套的数据框并将它们组合起来,结果如下desired_data:
###reproducing desired data
library(data.table)
sampledatain1$row <- 1
sampledatain1$colmerge1 <- "MA"
sampledatain1$colmerge2 <- -99
sampledatain2$row <- 2
sampledatain2$colmerge1 <- "MB"
sampledatain2$colmerge2 <- -98
sampledatain3$row <- 3
sampledatain3$colmerge1 <- "MC"
sampledatain3$colmerge2 <- -97
desired_data <- rbindlist(list(sampledatain1,sampledatain2,sampledatain3))
Run Code Online (Sandbox Code Playgroud)
如何使用函数从样本数据生成desired_data来做到这一点?
我正在处理每日 tif 文件(数千个每日文件)中的大量数据。我正在分析 shapefile 区域中栅格的平均值,在可能的数千个形状图层上重复。
我当前的 .tif 文件适用于整个国家/地区,而实际上我只需要每个 shapefile 图层的每个 tif 文件的一小部分区域。每个形状层都有一组不同的天数可供提取,即像这样的数据框:
df <- data.frame(shplayer=c("shp_layerbuffer1","shp_layerbuffer2", "shp_layerbuffer3"), start=c("2000_02_18", "2004_03_19", "2010_08_20"), end=c("2010_01_09", "2005_03_15", "2012_09_04"))
Run Code Online (Sandbox Code Playgroud)
在 R 中,有没有办法在读取文件之前裁剪 .tif(或任何光栅类型文件)?这样我就可以只读取每个 tif 文件的裁剪区域
我设想了类似的东西(在整个日期集上重复):
library(sf)
library(raster)
shp_layerbuffer1 <- st_read("myshpfolder", layer="shp_layerbuffer1", quiet=T)
###EXAMPLE BUT doesn't work to crop the raster as it comes in
tempraster <- raster::raster("mytif_2000_02_18.tif", ext=extent(shp_layerbuffer1))
Run Code Online (Sandbox Code Playgroud)
然后通常的velox(或光栅)提取,重复。
问题:是否有一个函数可以在尚未打开的情况下打开 selenium 浏览器,或者关闭当前浏览器并重置端口并重新启动浏览器?
理由:我在 RSelenium 中处理偶尔会崩溃的大循环,因此有时我不知道循环代码中端口是否打开或浏览器是否打开。我想要一个 RSelenium 启动器,无论浏览器是否打开或端口是否正在使用,它都会启动浏览器。
尝试:我使用 tryCatch() 尝试了这种方法,但如果我尝试启动它两次,我仍然会收到 wdman 错误,表明端口已打开:
browserpreference <- "chrome"
tryCatch({rD <- rsDriver(port = 4444L, browser = paste0(browserpreference))}
,error=function(rD,remDr){
try(remDr$close(), silent=T)
try(rD$server$stop(),silent=T)
try(suppressWarnings(rm(rD, envir = .GlobalEnv)), silent=T)
try(suppressWarnings(rm(remDr, envir = .GlobalEnv)), silent=T)
gc()
rD <- rsDriver(port = 4444L, browser = paste0(browserpreference))
})
Run Code Online (Sandbox Code Playgroud)
如果我尝试两次,我会收到此错误:
Error in wdman::selenium(port = port, verbose = verbose, version = version, :
Selenium server signals port = 4444 is already in use.
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有这样的数据:
library(data.table)
NN = 10000000
set.seed(32040)
DT <- data.table(
col = 1:10000000,
timestamp = 1521872652 + sample(7000001, NN, replace = TRUE)
)
Run Code Online (Sandbox Code Playgroud)
我试图将唯一的年份和周作为代码拉出来,这样我就可以对重复项进行排序(真正的数据表具有userID以及更多).我有一个当前的解决方案(下图),但是从日期列中唯一地粘贴数周和年份的部分很慢.日期的使用创建anytime包,拉week和year从lubridate仍然非常快.有人能帮我加快速度吗?谢谢!
我的慢代码(有效,但我想加快速度):
library(anytime)
library(lubridate)
tz<-"Africa/Addis_Ababa"
DT$localtime<- anytime(DT$timestamp, tz=tz) ###Lightning fast
DT$weekuni <- paste(year(DT$localtime),week(DT$localtime),sep="") ###super slow
Run Code Online (Sandbox Code Playgroud)
我的测试表明这是在paste扼杀我:
anytime到目前为止转换速度非常快
system.time(DT$localtime<- anytime(DT$timestamp, tz=tz)) ###Lightning fast
user system elapsed
0.264 0.417 0.933
Run Code Online (Sandbox Code Playgroud)
lubridate从日期开始快速的周和年转换,但速度很慢paste:
> system.time(DT$weekuni1 <- week(DT$localtime)) ###super slow
user system elapsed
1.203 …Run Code Online (Sandbox Code Playgroud)