小编Lim*_*ime的帖子

mutate 找不到函数

我正在通过这个网页的 eBird 代码工作:https : //github.com/CornellLabofOrnithology/ebird-best-practices/blob/master/03_covariates.Rmd

除了使用我自己的数据。我有一个来自澳大利亚 gadm.org 的 .gpkg,以及我自己为澳大利亚选择的电子鸟数据。我完全遵循了代码,除了不使用“bcr”,因为我的数据集没有 bcr 代码,同时st_buffer(dist = 10000)从 rgdal 代码中删除,因为这使我由于某种原因无法实际下载 MODIS 数据。

编辑:我还使用了网站提供的数据,但仍然收到相同的错误

我被这个代码困住了:

lc_extract <- ebird_buff %>% 
mutate(pland = map2(year_lc, data, calculate_pland, lc = landcover)) %>% 
select(pland) %>% 
unnest(cols = pland)
Run Code Online (Sandbox Code Playgroud)

它返回此错误:

Error: Problem with `mutate()` input `pland`.
x error in evaluating the argument 'x' in selecting a method for function 'exact_extract': invalid layer names
i Input `pland` is `map2(year_lc, data, calculate_pland, lc = landcover)`.)`
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚如何纠正它,我对这样的密集地理空间代码很陌生。

链接里有免费的数据集,但是我还没试过,所以可能是我的数据与代码不兼容?但是,我查看了提供的 Gis-data.gpkg,我的 gadm 数据似乎很好。 …

r function dplyr

5
推荐指数
1
解决办法
241
查看次数

使用 tidyverse 动词将以下函数翻译为基本 R 作为函数

我想在翻译中发现以下语法tidyversebaseR作为一个功能,虽然我有以下相同的输出困难。

这是语法:

x <- function(x) {x %>% 
    select(where(negate(is.numeric))) %>% 
    map_dfc(~ model.matrix(~ .x -1) %>% 
              as_tibble) %>% 
    rename_all(~ str_remove(., "\\.x")) 
}
Run Code Online (Sandbox Code Playgroud)

我知道这select可以表示为dataframe诸如x[,]. 至于管道函数%>%,我可以在变量中索引一个函数,即x <- ...

我可以设法转移 select(where(negate(is.numeric)))

进入:

x <- function(x){
  x[, !sapply(x, is.numeric)]
  
}
Run Code Online (Sandbox Code Playgroud)

虽然,这使它变得困难,因为我认为它可以用条件参数代替:

 map_dfc(~ model.matrix(~ .x -1)
Run Code Online (Sandbox Code Playgroud)

这是带有一些示例数据的预期输出:

# A tibble: 12 x 5
   black brown white female  male
   <dbl> <dbl> <dbl>  <dbl> <dbl>
 1     1     0     0      1     0
 2     1     0     0      1 …
Run Code Online (Sandbox Code Playgroud)

translation r tidyverse

3
推荐指数
2
解决办法
104
查看次数

向字符串添加唯一数字

我希望将一个数字附加到一个字符向量上,这样每一行都会收到一个与前一行不同的唯一数字,尽管相对于前一行是 +1。这样的n+1

我试过了:

test.all$ly_name <- sub("^", 1:nrow(test.all$ly_name), test.all$ly_name )
Run Code Online (Sandbox Code Playgroud)

它的样子:

1Prairie_Potholes          
1Prairie_Potholes          
1Prairie_Potholes 
.
.
.
Run Code Online (Sandbox Code Playgroud)

预期的:

Prairie_Potholes1          
Prairie_Potholes2          
Prairie_Potholes3 
.
.
.
Run Code Online (Sandbox Code Playgroud)

但是,这仅分配第一个数字 1。

可重现的代码:

c("Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes", 
"Prairie_Potholes", "Prairie_Potholes", "Boreal_Hardwood_Transition", 
"Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition", 
"Boreal_Hardwood_Transition", "Boreal_Hardwood_Transition")
Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
36
查看次数

在全局环境中隐藏数据

是否有允许数据隐藏在全局环境中但仍然可以访问的功能?

例如,我有一个长达 100 多行的非常长的脚本,我的全局环境看起来很混乱,太多了,我的大脑很难找到必要的东西。

我搜索了类似的问题,它们涉及创建一个包,坦率地说,我现在没有时间学习。

r

0
推荐指数
1
解决办法
218
查看次数

标签 统计

r ×4

dplyr ×1

function ×1

tidyverse ×1

translation ×1