我有 5 个不同长度的向量
a <- c(1) #with length of 1
b <- c(4.4,3.5) #length 2
c <- c(5.6,7.8,6.0) #length 3
d <- c(0.8,6.9,8.8,5.8) #length 4
e <- c(1.8,2.5,2.3,6.5,1.1) #length is 5
Run Code Online (Sandbox Code Playgroud)
我试图获取所有向量中元素的平均值:
#since there are 5 values available for 1st element
a[1]+b[1]+c[1]+d[1]+e[1] / 5
#since there are 4 values available for 2nd element
b[2]+c[2]+d[2]+e[2] / 4
#next divide by 3 and 2...1
c[3]+d[3]+e[3] / 3 and so on...
Run Code Online (Sandbox Code Playgroud)
我需要另一个数组中这些值的平均值,以便我可以进一步处理数据。
我正在尝试根据 R 中给定的位置和日期创建天体地图。
理想情况下,视觉效果应如下所示:
(来源)
我确实看到了这个博客,它使用了D3 天体图,并且对于创建下面的视觉效果非常有帮助。
library(sf)
library(tidyverse)
theme_nightsky <- function(base_size = 11, base_family = "") {
theme_light(base_size = base_size, base_family = base_family) %+replace%
theme(
# Specify axis options, remove both axis titles and ticks but leave the text in white
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(colour = "white",size=6),
# Specify legend options, here no legend is needed
legend.position = "none",
# Specify background of plotting area
panel.grid.major = …Run Code Online (Sandbox Code Playgroud) 对于某些对象,属性标识特殊列,例如对象中的几何列sf。为了在其中进行一些计算,dplyr最好能够轻松识别这些列。我正在寻找一种方法来创建一个有助于识别此列的函数。在下面的示例中,我可以创建一个函数来标识该列,但我仍然需要使用rlang拼接运算符 ( !!!)。
require(sf)\nrequire(dplyr)\nn<-4\ndf = st_as_sf(data.frame(x = 1:n, y = 1:n, cat=gl(2,2)), coords = 1:2, crs = 3857) %>% group_by(cat)\n# this is the example I start from however the geometry column is not guaranteed to have that name\ndf %>% mutate(d=st_distance(geometry, geometry[row_number()==1]))\n#> Simple feature collection with 4 features and 2 fields\n#> Geometry type: POINT\n#> Dimension: XY\n#> Bounding box: xmin: 1 ymin: 1 xmax: 4 ymax: 4\n#> Projected CRS: WGS 84 / Pseudo-Mercator\n#> # …Run Code Online (Sandbox Code Playgroud) 我正在努力...在特定的情况下化解我的论点,但我不明白为什么。
我可以创建一个这样的函数并...适当地化解:
library(dplyr)\nlibrary(tidyr)\n\nfill_na <- function(.x,...){\n\n dotArgs <- rlang::dots_list(...,.named=TRUE,.homonyms="last")\n tidyr::replace_na(.x,dotArgs) \n\n}\n\ndf <- tibble::tribble(\n ~colA, ~colB,\n "a", 1,\n "b", 2,\n "c", NA,\n NA, 4\n)\n\n> fill_na(df,colA="c",colB=2)\n# A tibble: 4 \xc3\x97 2\n colA colB\n <chr> <dbl>\n1 a 1\n2 b 2\n3 c 2\n4 c 4\nRun Code Online (Sandbox Code Playgroud)\n太好了,但是如果我做这个功能
\nmyFun <- function(inside_of,from_what, ... ,.metadata_defaults=list("Gender"="Undefined","Age"=35),.by_maxFormantHz=TRUE,.recompute=FALSE,.package="superassp"){\n\n dotArgs <- rlang::dots_list(...,.named=TRUE,.homonyms="last")\n return(1)\n\n}\nRun Code Online (Sandbox Code Playgroud)\n我得到这个结果:
\n> myFun(inside_of=ae,from_what=forest,fs=fm, fbw=bw)\nError in rlang::dots_list(..., .named = TRUE, .homonyms = "last") : \n object 'fm' not found\nRun Code Online (Sandbox Code Playgroud)\n为什么这里的争论没有被化解,而在第一个例子中却被化解了? …
我需要创建一个变量来计算每个 id 在过去 30 天内发生的观察次数。
例如,假设 id“a”在 1/2/2021 (d/m/y) 发生的观察。如果此观察是 2021 年 1 月 1 日至 2021 年 1 月 2 日期间 id“a”的第一个观察,则变量必须给出 1。如果是第二个,则为 2,依此类推。
这是一个更大的例子:
dat <- tibble::tribble(
~id, ~q, ~date,
"a", 1, "01/01/2021",
"a", 1, "01/01/2021",
"a", 1, "21/01/2021",
"a", 1, "21/01/2021",
"a", 1, "12/02/2021",
"a", 1, "12/02/2021",
"a", 1, "12/02/2021",
"a", 1, "12/02/2021",
"b", 1, "02/02/2021",
"b", 1, "02/02/2021",
"b", 1, "22/02/2021",
"b", 1, "22/02/2021",
"b", 1, "13/03/2021",
"b", 1, "13/03/2021",
"b", 1, "13/03/2021",
"b", 1, "13/03/2021") …Run Code Online (Sandbox Code Playgroud) 我有一个数据帧 df_3 ,我想从中改变以Team_开头的多个列。我想用 NA 替换列中包含的 0。我使用以前成功使用过的代码,但现在出现以下错误:
\nError in `mutate()`:\n\xe2\x84\xb9 In argument: `across(starts_with("Team_"), ~na_if(., "0"))`.\nCaused by error in `across()`:\n! Can't compute column `Team_Num_1`.\nCaused by error in `na_if()`:\n! Can't convert `y` <character> to match type of `x` <double>.\nBacktrace:\n 1. df_3 %>% mutate(across(starts_with("Team_"), ~na_if(., "0")))\n 10. dplyr::na_if(Team_Num_1, "0")\nRun Code Online (Sandbox Code Playgroud)\n知道为什么会这样或者我该如何解决它吗?我没有更改原始 df 中的任何内容以及之前运行的代码,不确定发生了什么变化。
\n可复制代码:
\nstructure(list(Team_1 = c("0", "werg", "sdf"), Team_Desc_1 = c("wer", \n"wtrb", "wergt"), Team_URL_1 = c("ewrg", "werg", "asd"), Team_Ver_1 = c("25", \n"2523", "342"), Team_Num_1 = c(0, 23, 12), …Run Code Online (Sandbox Code Playgroud) 如何证明这两个函数相等?
\na<-function(x) sec^2(x)\nb<-function(x) 1+tan^2(x)\nRun Code Online (Sandbox Code Playgroud)\n我尝试使用identical但它\xe2\x80\x99s 显示错误。
identical(a,b)\n[1] FALSE\nRun Code Online (Sandbox Code Playgroud)\n 如何ifelse()使用存储在字符向量中的条件评估语句?
例如:
a <- 1
b <- 2
condition <- ">"
ifelse(a condition b, print("good"), print("bad))
Run Code Online (Sandbox Code Playgroud) 我从 XML 文件中抓取了一些文本块,这些文本块经常缺少句子之间的空格。我已经str_split成功地将这些块分解成易于理解的句子,如下所示:
list_of_strings <- str_split(chunk_of_text, pattern=boundary("sentence")
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但它不能处理终止句号后面没有空格的情况。例如,"This sentence ends.This sentence continues." 它返回 1 个句子,而不是两个。
使用str_splitwithpattern=boundary("sentence")不起作用。
如果我搜索句点并将其替换为句点空格,当然会弄乱 1.5 磅之类的数字。
我探索过使用通配符来检测情况,例如,
str_view_all(x, "[[:alpha:]]\\.[[:alpha:]]"))
Run Code Online (Sandbox Code Playgroud)
但我不知道如何 1) 在句点后插入一个空格,以便后续对 str_split 的调用正常工作,或 2) 在句点处拆分。
发生这种情况时,有什么关于分隔句子的建议吗?
R程序员新手,感谢您的帮助!
我有数据框显示不同的温度参数作为每日日期时间的函数,就像这样:
| YEAR | DOY | T2M | TS |
| 1991 | 1 | 25.69 | 25.76 |
| 1991 | 2 | 23.12 | 21.46 |
| 1991 | 3 | 21.65 | 22.56 |
| ---- | ---| ----- | ---- |
| 1991 |300 | 21.49 | 22.75 |
| 1991 |301 | 21.49 | 22.75 |
| 1991 |302 | 21.49 | 22.75 |
Run Code Online (Sandbox Code Playgroud)
我想将 YEAR 和 DOY(一年中的日期)列转换为 R 中的日期时间单列,日期格式为 dd/mm/yyyy
| DATE | T2M …Run Code Online (Sandbox Code Playgroud)