问题列表 - 第318117页

按组计算列中 1 出现的间隔年数

我已将这两个代码建议应用到我的原始数据集来比较peace1 和peace2。

和平1:

d_muslim <- d_muslim %>% 
   mutate(last_conflict = lag(if_else(conflict == 1, year,NA),default = min(year) - 1),
   .by = country) %>%     
   tidyr::fill(last_conflict, .direction = "down") %>% 
   mutate(peace1 = year - last_conflict - 1)
Run Code Online (Sandbox Code Playgroud)

和平2:

d_muslim <- d_muslim %>%
  mutate(helper = cumsum(lag(conflict, default = 1) == 1),.by = country) %>% 
  mutate(peace2 = year - first(year), .by = c(country, helper)) %>%
  select(-helper)
Run Code Online (Sandbox Code Playgroud)

结果:

国家 冲突 和平1 和平2
伊朗 1946年 0 -73 0
伊朗 1947年 0 -72 1
伊朗 1948年 0 -71 …

r dataframe dplyr

4
推荐指数
1
解决办法
158
查看次数

为什么我无法从对带有 String 的结构体的引用的迭代器中收集 Vec&lt;&amp;str&gt; ?

我目前正在学习 Rust,我想从拥有字符串的项目向量中收集字符串 (&strs) 的引用。

struct Channel {
    cid: usize,
    name: String,
}

fn main() {
    let channels: Vec<Channel> = vec![Channel {
        cid: 1,
        name: "Hello".to_string(),
    }];
    let names: Vec<&str> = channels.iter().map(|x| &x.name).collect();
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译,出现以下错误:

[E0277]: a value of type `Vec<&str>` cannot be built from an iterator over elements of type `&String`
    --> src/main.rs:11:61
     |
11   |     let names: Vec<&str> = channels.iter().map(|x| &x.name).collect();
     |                                                             ^^^^^^^ value of type `Vec<&str>` cannot be built from `std::iter::Iterator<Item=&String>`
     |
     = help: the trait `FromIterator<&String>` is …
Run Code Online (Sandbox Code Playgroud)

rust

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

Any where Str|True 的子集名称:为什么名称类型与每个类型匹配?

我预计将 Any 类型细化为 Str|True 将使name类型与任何 Str 或 True 匹配,但这就是我所看到的:

\n
subset name of Any where Str|True;\n\nsub go(name :$x) {\n   say $x;\n}\n\ngo(x => "hello");  #hello\ngo(x => True);     #True\n\ngo(x => 2);        #2\nmy @arr = 1, 2, 3; \ngo(x => @arr);     #[1, 2, 3]\n
Run Code Online (Sandbox Code Playgroud)\n

如果我将子集更改为subset name of Any where Str|Bool,那么它会按我的预期工作:

\n
subset name of Any where Str|Bool;\n\nsub go(name :$x) {\n   say $x;\n}\n\ngo(x => "hello");   \ngo(x => True);      \ngo(x => False);\n\ngo(x => 2);\n\n--output:--\nhello\nTrue\nFalse\nConstraint type check failed in binding to …
Run Code Online (Sandbox Code Playgroud)

raku

6
推荐指数
1
解决办法
106
查看次数

弃用 Marker 类 - Google Map API 警告

我正在开发一个带有 google 地图 typescript api 的应用程序,今天我在应用程序启动时收到此警告消息 ->

自 2024 年 2 月 21 日起,google.maps.Marker 将不再可用。请改用 google.maps.marker.AdvancedMarkerElement。有关终止的更多信息,请访问 https://developers.google.com/maps/deprecations

但 AdvancedMarkerElement 没有具有 Marker 类的所有属性...为什么呢?我将 @types/google.maps 更新为最新版本,但我不断收到相同的警告,有人知道这件事吗?以及如何解决?

google-maps google-maps-api-3 typescript

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

为什么分配有 time.Afterfunc 的 Timer 变量即使外部 func 退出也会执行其函数

我有一个永远循环,它有一个 Timer 变量,在一段时间后执行一个函数。

package main

import (
    fmt "fmt"
    "time"
)

func exe() {
    fmt.Println("10-seconds-of-time-is-done")
}

func t() {
    var timerCheck *time.Timer
    fmt.Println("here's the timerCheck", timerCheck)
    for {
        timerCheck = time.AfterFunc(10*time.Second, func() { exe() })
        fmt.Println("exiting-from-function-t")
        return
    }
}

func main() {
    t()
    fmt.Println("waiting-inside-main")
    time.Sleep(20 * time.Second)
}

Run Code Online (Sandbox Code Playgroud)

这是输出。我不明白该函数t()立即返回,而是在 10 秒后timerCheck执行其exe()函数。

here's the timerCheck <nil>
exiting-from-function-t
waiting-inside-main
10-seconds-of-time-is-done
Run Code Online (Sandbox Code Playgroud)

这怎么可能?在标准库中,关于AfterFunc

// AfterFunc waits for the duration to elapse and then calls f
// in …
Run Code Online (Sandbox Code Playgroud)

concurrency timer go goroutine

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

求减法结果平方的最快方法?

我需要对减法结果进行平方,并且需要速度快。

我已经确定了一些可能的方法,但我想听听优点和缺点

unsigned int a = 5;
unsigned int b = 7;


/* Approach 1a */
unsigned int c  = (a - b) * (a - b);

/* Approach 1b */
int d = (a - b);
unsigned int e = d * d;


/* Approach 2a */
unsigned int f = abs(a - b) * abs(a - b);

/* Approach 2b */
unsigned int g = abs((a - b) * (a - b));

/* Approach 2c */
unsigned int …
Run Code Online (Sandbox Code Playgroud)

c

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

极坐标中的 df.drop_duplicates() ?

import polars as pl

df = pl.DataFrame(
    {
        "X": [4, 2, 3, 4],
        "Y": ["p", "p", "p", "p"],
        "Z": ["b", "b", "b", "b"],
    }
)
Run Code Online (Sandbox Code Playgroud)

的等价物df.drop_duplicates()df.unique()

但是,每次执行查询时都会得到不同的结果?

print(df.unique())
Run Code Online (Sandbox Code Playgroud)
X   Y   Z
i64 str str
3   "p" "b"
2   "p" "b"
4   "p" "b"
Run Code Online (Sandbox Code Playgroud)
X   Y   Z
i64 str str
4   "p" "b"
2   "p" "b"
3   "p" "b"
Run Code Online (Sandbox Code Playgroud)
X   Y   Z
i64 str str
2   "p" "b"
3   "p" "b"
4   "p" "b"
Run Code Online (Sandbox Code Playgroud)

这是故意的吗?背后的原因是什么?

python unique dataframe python-3.x python-polars

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

std::array 模板的运算符重载?

我想要一种简单的方法来将固定大小的浮点数组按元素相乘。我编写了以下代码来尝试实现此目的:

\n
#include <array>\n#include <cstdint>\n\ntemplate <uint16_t N>\nusing color = std::array<float,N>;\n\ntemplate <uint16_t N>\nconstexpr color<N> operator* (color<N> lhs, const color<N>& rhs)\n{\n    for (int i = 0; i < N; i++){\n        lhs[i] *= rhs[i];\n    }\n    return lhs;\n};\n\nint main()\n{\n    constexpr uint16_t N = 10;\n\n    color<N> myColor{1,2,3,4,5,6,7,8,9,10};\n    color<N> myColor2{2,4,6,8,10,12,14,16,18,20};\n    \n    color<N> mult = myColor * myColor2;\n\n    return 0;\n};\n
Run Code Online (Sandbox Code Playgroud)\n

使用 gcc 编译时,出现以下错误:

\n
test.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ntest.cpp:23:29: error: no match for \xe2\x80\x98operator*\xe2\x80\x99 (operand types are \xe2\x80\x98color<10>\xe2\x80\x99 {aka \xe2\x80\x98std::array<float, 10>\xe2\x80\x99} and \xe2\x80\x98color<10>\xe2\x80\x99 {aka \xe2\x80\x98std::array<float, 10>\xe2\x80\x99})\n …
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates operator-overloading

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

R - 用矩阵重塑列表

我正在处理从以下输出创建的矩阵列表combnlapply(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)

r matrix reshape

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

如何在 Rust 中使用传递依赖的特征?

如何在我的应用程序中使用传递依赖项箱中的特征?

这是我遇到的问题的一个最小说明示例:

在 中Cargo.toml,我有:

[dependencies]
mersenne_twister = "1.1.1"
rand = "0.8.5"
Run Code Online (Sandbox Code Playgroud)

我的箱子取决于rand ^0.8.5mersenne_twister ^1.1.1,它本身取决于rand >=0.3, <0.5

my-crate ---> rand 0.8.5
         |
         |
         ---> mersenne_twister 1.1.1  ----> rand >= 0.3, < 0.5
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,我想使用trait rand::Rng for mersenne_twister::MT19937. 但是当我尝试将这个特征纳入范围时,它显然没有被识别:

my-crate ---> rand 0.8.5
         |
         |
         ---> mersenne_twister 1.1.1  ----> rand >= 0.3, < 0.5
Run Code Online (Sandbox Code Playgroud)

我的猜测是,Rng导入的特征use rand::Rng;来自 的特征rand 0.8.5,而不是实际实现的特征rand 0.4.6MT19937,即使它们的拼写方式相同,但它们是不同且不相关的特征,因此不能互换引用。


所以我有一些问题:

  1. 如何在我的应用程序中使用Rng …

dependencies traits rust transitive-dependency semantic-versioning

4
推荐指数
2
解决办法
74
查看次数