如何计算r中两年的移动平均值

2024-04-23

我有一个关于并购 (M&As) 的大数据框(900k 行)。

df 有四列:date(并购完成时),目标国家(被合并/收购的国家的公司),收购方_国家(收购方是哪个国家的公司),以及big_corp(无论收购方是否是大公司,其中 TRUE 表示该公司很大)。

这是我的 df 的示例:

> df <- structure(list(date = c(2000L, 2000L, 2001L, 2001L, 2001L, 2002L, 
2002L, 2002L), target_nation = c("Uganda", "Uganda", "Uganda", 
"Uganda", "Uganda", "Uganda", "Uganda", "Uganda"), acquiror_nation = c("France", 
"Germany", "France", "France", "Germany", "France", "France", 
"Germany"), big_corp_TF = c(TRUE, FALSE, TRUE, FALSE, FALSE, 
TRUE, TRUE, TRUE)), row.names = c(NA, -8L))

> df 

   date target_nation acquiror_nation big_corp_TF
1: 2000        Uganda          France        TRUE
2: 2000        Uganda         Germany       FALSE
3: 2001        Uganda          France        TRUE
4: 2001        Uganda          France       FALSE
5: 2001        Uganda         Germany       FALSE
6: 2002        Uganda          France        TRUE
7: 2002        Uganda          France        TRUE
8: 2002        Uganda         Germany        TRUE

根据这些数据,我想创建一个新变量,表示特定收购方国家的大公司进行的并购的份额,计算2年的平均值。(对于我的实际练习,我将计算 5 年的平均值,但让我们在这里简单一点)。因此,法国的大公司将会有一个新的变量,德国的大公司也会有一个新的变量。

到目前为止,我所做的是1)统计某一年特定目标国家的并购总数; 2)统计某一收购方国家的大公司在某一年在特定目标国家进行的并购总数。我加入了这两个dfs以方便计算我想要的平均值。这是我使用的代码和生成的新 df:

##counting total rows for target nations
df2 <- df %>%
 group_by(date, target_nation) %>%
 count(target_nation)

##counting total rows conducted by small or big corps for certain acquiror nations

df3 <- df %>%
  group_by(date, target_nation, acquiror_nation) %>%
  count(big_corp_TF)

##selecting rows that were conducted by big corps

df33 <- df3 %>%
  filter(big_corp_TF == TRUE)

##merging df2 and df33

df4 <- df2 %>%
  left_join(df33, by = c("date" = "date", "target_nation" = "target_nation"))

df4 <- as.data.frame(df4)

> df4

  date target_nation n.x acquiror_nation big_corp_TF n.y
1 2000        Uganda   2          France        TRUE   1
2 2001        Uganda   3          France        TRUE   1
3 2002        Uganda   3          France        TRUE   2
4 2002        Uganda   3         Germany        TRUE   1

这里的 n.x 是某一年特定 target_nation 的并购总数(行); n.y 是特定收购者国家/地区的大公司在特定目标国家/地区进行的并购总数(行)。

有了这个新的数据框架 df4,我现在可以轻松计算特定收购方国家的大公司在特定年份在特定目标国家中进行的并购的份额。例如,让我们计算一下法国的这一份额:

df5 <- df4 %>% 
  filter(acquiror_nation == "France") %>%
  mutate(France_bigcorp_share_1year = n.y / n.x)

  date target_nation n.x acquiror_nation big_corp_TF n.y France_bigcorp_share_1year
1 2000        Uganda   2          France        TRUE   1                  0.5000000
2 2001        Uganda   3          France        TRUE   1                  0.3333333
3 2002        Uganda   3          France        TRUE   2                  0.6666667

然而,我无法弄清楚如何计算特定收购方国家的大公司进行的并购的份额,计算平均数2 years.

这就是所需变量的样子:

  date target_nation n.x acquiror_nation big_corp_TF n.y France_bigcorp_share_2years
1 2000        Uganda   2          France        TRUE   1                  0.5000000
2 2001        Uganda   3          France        TRUE   1                  0.4000000
3 2002        Uganda   3          France        TRUE   2                  0.5000000

请注意,2000 年的份额将保持不变,因为没有前一年可以使其成为 2 年平均值; 2001 年它将变为 0.4(因为 (1+1)/(2+3) = 0.4); 2002 年它将变为 0.5(因为 (1+2)/(3+3) = 0.5)。

您知道如何编写计算两年平均份额的代码吗?我想我需要在这里使用 for 循环,但我不知道如何使用。任何建议,将不胜感激。

--

EDIT:AnilGoyal 的代码与示例数据完美配合,但我的实际数据显然更混乱,因此我想知道是否有解决我遇到的问题的方法。

我的实际数据集有时会跳过一年,或者有时不包括前几行中包含的 acquiror_nations。请查看我的实际数据的更准确样本:

> df_new <- structure(list(date = c(2000L, 2000L, 2001L, 2001L, 2001L, 2002L, 
2002L, 2002L, 2003L, 2003L, 2004L, 2004L, 2004L, 2006L, 2006L
), target_nation = c("Uganda", "Uganda", "Uganda", "Uganda", 
"Uganda", "Uganda", "Uganda", "Uganda", "Uganda", "Uganda", "Uganda", 
"Uganda", "Uganda", "Uganda", "Uganda"), acquiror_nation = c("France", 
"Germany", "France", "France", "Germany", "France", "France", 
"Germany", "Germany", "Germany", "France", "France", "Germany", 
"France", "France"), big_corp_TF = c(TRUE, FALSE, TRUE, FALSE, FALSE, 
TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE)), row.names = c(NA, 
-15L))

> df_new 

    date target_nation acquiror_nation big_corp_TF
 1: 2000        Uganda          France     TRUE
 2: 2000        Uganda         Germany    FALSE
 3: 2001        Uganda          France     TRUE
 4: 2001        Uganda          France    FALSE
 5: 2001        Uganda         Germany    FALSE
 6: 2002        Uganda          France     TRUE
 7: 2002        Uganda          France     TRUE
 8: 2002        Uganda         Germany     TRUE
 9: 2003        Uganda         Germany     TRUE
10: 2003        Uganda         Germany    FALSE
11: 2004        Uganda          France     TRUE
12: 2004        Uganda          France    FALSE
13: 2004        Uganda         Germany     TRUE
14: 2006        Uganda          France     TRUE
15: 2006        Uganda          France     TRUE

注意:2003 年法国没有任何行;并且没有2005年。

如果我运行 Anil 的第一个代码,结果如下:

   date target_nation acquiror_nation    n1    n2 share
  <int> <chr>         <chr>           <dbl> <int> <dbl>
1  2000 Uganda        France              2     1   0.5
2  2001 Uganda        France              3     1   0.4
3  2002 Uganda        France              3     2   0.5
4  2004 Uganda        France              3     1   0.5
5  2006 Uganda        France              2     2   0.6

注:法国 2003 年和 2005 年没有结果;我希望有 2003 年和 2005 年的结果(因为我们正在计算 2 年平均值,因此我们应该能够得到 2003 年和 2005 年的结果)。另外,2006 年的份额实际上是不正确的,因为它应该是 1(应该采用 2005 年的值(均为 0)而不是 2004 年的值来计算平均值)。

我希望能够收到以下内容:

       date target_nation acquiror_nation    n1    n2 share
      <int> <chr>         <chr>           <dbl> <int> <dbl>
    1  2000 Uganda        France              2     1   0.5
    2  2001 Uganda        France              3     1   0.4
    3  2002 Uganda        France              3     2   0.5
    4  2003 Uganda        France              2     0   0.4
    5  2004 Uganda        France              3     1   0.2
    6  2005 Uganda        France              0     0   0.33
    7  2006 Uganda        France              2     2   1.0

注意:请注意 2006 年的结果也不同(因为我们现在采用 2005 年而不是 2004 年作为两年平均值)。

您认为有可能找到一种方法来输出所需的小标题吗?我知道这是原始数据的问题:它只是缺少某些数据点。然而,将它们包含到原始数据集中似乎非常不方便;最好将它们包含在中间,例如计算完n1和n2后。但最方便的方法是什么?

EDIT2:Anil 的新代码可以很好地处理上面的数据样本,但在处理更复杂的数据样本(包括多个 target_nation)时,它会遇到一个不受欢迎的问题。这是一个更短但更复杂的数据样本:

> df_new_complex <- structure(list(date = c(2000L, 2000L, 2001L, 2001L, 2001L, 2003L, 
2003L, 1999L, 2001L, 2002L, 2002L), target_nation = c("Uganda", 
"Uganda", "Uganda", "Uganda", "Uganda", "Uganda", "Uganda", "Mozambique", 
"Mozambique", "Mozambique", "Mozambique"), acquiror_nation = c("France", 
"Germany", "France", "France", "Germany", "Germany", "Germany", 
"Germany", "France", "France", "Germany"), big_corp_TF = c(TRUE, 
FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE
)), row.names = c(NA, -11L))

> df_new_complex 

date target_nation acquiror_nation big_corp_TF
 1: 2000        Uganda          France        TRUE
 2: 2000        Uganda         Germany       FALSE
 3: 2001        Uganda          France        TRUE
 4: 2001        Uganda          France       FALSE
 5: 2001        Uganda         Germany       FALSE
 6: 2003        Uganda         Germany        TRUE
 7: 2003        Uganda         Germany       FALSE
 8: 1999    Mozambique         Germany       FALSE
 9: 2001    Mozambique          France        TRUE
10: 2002    Mozambique          France       FALSE
11: 2002    Mozambique         Germany        TRUE

如您所见,此数据样本包含两个 target_nations。阿尼尔的代码,其中param <- c("France", "Germany"),产生以下小标题:

    date target_nation acquiror_nation    n1    n2 share
   <dbl> <chr>         <chr>           <dbl> <int> <dbl>
 1  1999 Mozambique    France              1     0 0    
 2  1999 Mozambique    Germany             1     0 0    
 3  1999 Uganda        France              0     0 0    
 4  1999 Uganda        Germany             0     0 0    
 5  2000 Mozambique    France              0     0 0    
 6  2000 Mozambique    Germany             0     0 0    
 7  2000 Uganda        France              2     1 0.25 
 8  2000 Uganda        Germany             2     0 0.167
 9  2001 Mozambique    France              1     1 0.4  
10  2001 Mozambique    Germany             1     0 0.333
11  2001 Uganda        France              3     1 0.333
12  2001 Uganda        Germany             3     0 0.25 
13  2002 Mozambique    France              2     0 0.2  
14  2002 Mozambique    Germany             2     1 0.25 
15  2002 Uganda        France              0     0 0.25 
16  2002 Uganda        Germany             0     0 0.25 
17  2003 Mozambique    France              0     0 0.25 
18  2003 Mozambique    Germany             0     0 0.25 
19  2003 Uganda        France              2     0 0.167
20  2003 Uganda        Germany             2     1 0.25 

这里不受欢迎的是,该代码为乌干达创建了 1999 年,为莫桑比克创建了 2003 年(后者问题不大)。 1999 年,乌干达没有数据样本中所示的投资,因此拥有数值是没有意义的(它可能有 NA,或者根本不存在)。莫桑比克在2003年也没有投资,所以我不想计算莫桑比克当年的份额。

我找到了一个解决方法,我可以在代码的早期过滤特定的目标国家,就像这样:

correct1 <- df_new_complex %>% 
  filter(target_nation == "Mozambique") %>%
  mutate(d = 1) %>% ...

#I do the same for another target_nation

correct2 <- df_new_complex %>% 
  filter(target_nation == "Uganda") %>%
  mutate(d = 1) %>% ...

#I then use rbind

correct <- rbind(correct1, correct2)

#which produces the desired tibble (without a year 2003 for Mozambique and 1999 for Uganda).

> correct 

date target_nation acquiror_nation    n1    n2 share
   <dbl> <chr>         <chr>           <dbl> <int> <dbl>
 1  1999 Mozambique    France              1     0 0    
 2  1999 Mozambique    Germany             1     0 0    
 3  2000 Mozambique    France              0     0 0    
 4  2000 Mozambique    Germany             0     0 0    
 5  2001 Mozambique    France              1     1 1    
 6  2001 Mozambique    Germany             1     0 0 
 7  2002 Mozambique    France              2     0 0.33 
 8  2002 Mozambique    Germany             2     1 0.333
 9  2000 Uganda        France              2     1 0.5  
10  2000 Uganda        Germany             2     0 0.25 
11  2001 Uganda        France              3     1 0.286
12  2001 Uganda        Germany             3     0 0.2  
13  2002 Uganda        France              0     0 0.167
14  2002 Uganda        Germany             0     0 0.167
15  2003 Uganda        France              2     0 0    
16  2003 Uganda        Germany             2     1 0.25 

什么是更快的方法来做到这一点?我有一份所需的 target_nations 列表。也许可以创建一个循环,我可以通过该循环对一个 target_nation 进行计算,然后对另一个进行计算;然后将它们绑定;然后是另一个;然后rbind等。或者有更好的方法吗?


带包runner你可以做这样的事情

df <- structure(list(date = c(2000L, 2000L, 2001L, 2001L, 2001L, 2002L, 
                              2002L, 2002L), target_nation = c("Uganda", "Uganda", "Uganda", 
                                                               "Uganda", "Uganda", "Uganda", "Uganda", "Uganda"), acquiror_nation = c("France", 
                                                                                                                                      "Germany", "France", "France", "Germany", "France", "France", 
                                                                                                                                      "Germany"), big_corp_TF = c(TRUE, FALSE, TRUE, FALSE, FALSE, 
                                                                                                                                                                  TRUE, TRUE, TRUE)), row.names = c(NA, -8L))

library(runner)
library(tidyverse)
df <- df %>% as.data.frame()
param <- 'France'
df %>% 
  group_by(date, target_nation) %>%
  mutate(n1 = n()) %>%
  group_by(date, target_nation, acquiror_nation) %>%
  summarise(n1 = mean(n1),
            n2 = sum(big_corp_TF), .groups = 'drop') %>%
  filter(acquiror_nation == param) %>%
  mutate(share = sum_run(n2, k=2)/sum_run(n1, k=2))
#> # A tibble: 3 x 6
#>    date target_nation acquiror_nation    n1    n2 share
#>   <int> <chr>         <chr>           <dbl> <int> <dbl>
#> 1  2000 Uganda        France              2     1   0.5
#> 2  2001 Uganda        France              3     1   0.4
#> 3  2002 Uganda        France              3     2   0.5

即使你可以同时为所有国家做


df %>% 
  group_by(date, target_nation) %>%
  mutate(n1 = n()) %>%
  group_by(date, target_nation, acquiror_nation) %>%
  summarise(n1 = mean(n1),
            n2 = sum(big_corp_TF), .groups = 'drop') %>%
  group_by(acquiror_nation) %>%
  mutate(share = sum_run(n2, k=2)/sum_run(n1, k=2))
#> # A tibble: 6 x 6
#> # Groups:   acquiror_nation [2]
#>    date target_nation acquiror_nation    n1    n2 share
#>   <int> <chr>         <chr>           <dbl> <int> <dbl>
#> 1  2000 Uganda        France              2     1 0.5  
#> 2  2000 Uganda        Germany             2     0 0    
#> 3  2001 Uganda        France              3     1 0.4  
#> 4  2001 Uganda        Germany             3     0 0    
#> 5  2002 Uganda        France              3     2 0.5  
#> 6  2002 Uganda        Germany             3     1 0.167

鉴于修改后的场景,您需要做两件事 -

  • 包括参数idx = date同时sum_run功能。这将根据需要更正输出,但不包括缺失行/年份的份额。
  • 要包括缺失的年份,您需要tidyr::complete如下所示-
param <- 'France'
df_new %>% 
  mutate(d = 1) %>%
  complete(date = seq(min(date), max(date), 1), nesting(target_nation, acquiror_nation),
           fill = list(d =0, big_corp_TF = FALSE)) %>%
  group_by(date, target_nation) %>%
  mutate(n1 = sum(d)) %>%
  group_by(date, target_nation, acquiror_nation) %>%
  summarise(n1 = mean(n1),
            n2 = sum(big_corp_TF), .groups = 'drop') %>%
  filter(acquiror_nation == param) %>%
  mutate(share = sum_run(n2, k=2, idx = date)/sum_run(n1, k=2, idx = date))

# A tibble: 7 x 6
   date target_nation acquiror_nation    n1    n2 share
  <dbl> <chr>         <chr>           <dbl> <int> <dbl>
1  2000 Uganda        France              2     1 0.5  
2  2001 Uganda        France              3     1 0.4  
3  2002 Uganda        France              3     2 0.5  
4  2003 Uganda        France              2     0 0.4  
5  2004 Uganda        France              3     1 0.2  
6  2005 Uganda        France              0     0 0.333
7  2006 Uganda        France              2     2 1

与上面类似,您可以一次对所有国家执行此操作(按组替换过滤器)

df_new %>% 
  mutate(d = 1) %>%
  complete(date = seq(min(date), max(date), 1), nesting(target_nation, acquiror_nation),
           fill = list(d =0, big_corp_TF = FALSE)) %>%
  group_by(date, target_nation) %>%
  mutate(n1 = sum(d)) %>%
  group_by(date, target_nation, acquiror_nation) %>%
  summarise(n1 = mean(n1),
            n2 = sum(big_corp_TF), .groups = 'drop') %>%
  group_by(acquiror_nation) %>%
  mutate(share = sum_run(n2, k=2, idx = date)/sum_run(n1, k=2, idx = date))

# A tibble: 14 x 6
# Groups:   acquiror_nation [2]
    date target_nation acquiror_nation    n1    n2 share
   <dbl> <chr>         <chr>           <dbl> <int> <dbl>
 1  2000 Uganda        France              2     1 0.5  
 2  2000 Uganda        Germany             2     0 0    
 3  2001 Uganda        France              3     1 0.4  
 4  2001 Uganda        Germany             3     0 0    
 5  2002 Uganda        France              3     2 0.5  
 6  2002 Uganda        Germany             3     1 0.167
 7  2003 Uganda        France              2     0 0.4  
 8  2003 Uganda        Germany             2     1 0.4  
 9  2004 Uganda        France              3     1 0.2  
10  2004 Uganda        Germany             3     1 0.4  
11  2005 Uganda        France              0     0 0.333
12  2005 Uganda        Germany             0     0 0.333
13  2006 Uganda        France              2     2 1    
14  2006 Uganda        Germany             2     0 0

进一步编辑

  • 这很容易。消除,target_nation from nesting并添加一个group_by之前在上面complete.

简单的。不是吗

df_new_complex %>%
  mutate(d = 1) %>%
  group_by(target_nation) %>%
  complete(date = seq(min(date), max(date), 1), nesting(acquiror_nation),
           fill = list(d =0, big_corp_TF = FALSE)) %>%
  group_by(date, target_nation) %>%
  mutate(n1 = sum(d)) %>%
  group_by(date, target_nation, acquiror_nation) %>%
  summarise(n1 = mean(n1),
            n2 = sum(big_corp_TF), .groups = 'drop') %>%
  group_by(acquiror_nation) %>%
  mutate(share = sum_run(n2, k=2)/sum_run(n1, k=2))

# A tibble: 16 x 6
# Groups:   acquiror_nation [2]
    date target_nation acquiror_nation    n1    n2 share
   <dbl> <chr>         <chr>           <dbl> <int> <dbl>
 1  1999 Mozambique    France              1     0 0    
 2  1999 Mozambique    Germany             1     0 0    
 3  2000 Mozambique    France              0     0 0    
 4  2000 Mozambique    Germany             0     0 0    
 5  2000 Uganda        France              2     1 0.5  
 6  2000 Uganda        Germany             2     0 0    
 7  2001 Mozambique    France              1     1 0.667
 8  2001 Mozambique    Germany             1     0 0    
 9  2001 Uganda        France              3     1 0.5  
10  2001 Uganda        Germany             3     0 0    
11  2002 Mozambique    France              2     0 0.2  
12  2002 Mozambique    Germany             2     1 0.2  
13  2002 Uganda        France              0     0 0    
14  2002 Uganda        Germany             0     0 0.5  
15  2003 Uganda        France              2     0 0    
16  2003 Uganda        Germany             2     1 0.5 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何计算r中两年的移动平均值 的相关文章

  • 使用 R 和 rvest 进行网页抓取

    我正在尝试rvest学习使用 R 进行网页抓取 我正在尝试为页面的其他几个部分复制乐高示例并使用selector gadget to id 我从中提取了示例R Studio 教程 http blog rstudio org 2014 11
  • 控制绘图中 x 轴上出现哪些刻度线/标签?

    我想控制 X 轴上显示的刻度线 以下代码将刻度线放置在 5 的序列中 位于 5 10 15 30 library plotly df lt data frame x 1 30 y sample 100 300 size 30 replace
  • rbind 命名向量到不同长度的矩阵

    我正在尝试将命名向量绑定到矩阵上 命名向量的长度与矩阵不同 gt m lt matrix data c 1 2 3 nrow 1 ncol 3 dimnames list c c column 1 column 2 column 3 gt
  • 每次向量元素变化时在 R 中分割向量

    每次元素值发生变化时 我都需要分割重复元素组的向量 例如 test vector lt c string1 string1 string1 string2 string2 string1 string1 string3 必须成为 1 1 s
  • 比较具有不同顶点数的图中的社区

    我正在根据通信数据图计算鲁汶社区 其中顶点代表大型项目的执行者 这些图表代表不同的通信方式 例如电子邮件 电话 我们想尝试从通信数据中识别表演者团队 由于表演者对不同的通信方法有不同的偏好 因此图的大小不同 并且可能有一些独特的顶点 而这些
  • 在 r 中使用 reprex 包创建可重现的示例,其中正在读取本地文件

    我经常使用reprex reprex创建可重复的示例R代码以获得其他人的帮助以消除我的代码中的错误 通常 我使用数据集创建最小的示例 例如iris or mtcars而且效果很好 但我总是无法使用reprex任何时候我需要使用我的own数据
  • 对整数进行反直觉测试:63 = (45 x 1.4) = 62

    我写了一个 可能不是特别好 函数来测试一个数字是否是整数 is wholeNumber lt function x x floor x 一般来说 这个函数对我的目的来说效果很好 因为我实际上只考虑用少数小数位测试数字的情况 所以我天真的理解
  • 在 Apache 服务器上运行 R Shiny 应用程序

    我有一个闪亮的应用程序 我想在现有的 Apache 服务器上运行 我对服务器完全陌生 不太知道从哪里开始 不幸的是 我在网上找到的大多数资源都是关于 NGINX 而不是 Apache 我知道这样的事情是可能的 但我不知道如何开始 如何开始在
  • tidyr:在函数内使用 mutate

    我想使用 tidyverse 中的 mutate 函数来基于旧列创建一个新列 仅使用数据框和字符串 代表列标题 作为输入 我可以在不使用 tidyverse 的情况下让它工作 参见下面的函数 f 但我想使用 tidyverse 让它工作 参
  • 向数据集中选定的一组列名称添加后缀

    我想向数据集 CTDB 中的一组列添加后缀 例如 我有以下列 我想在末尾添加 Child 该子集是包含 100 多列的较大数据集的一部分 我不想重写每个列名称 9 SCARED BREATHE 10 SCARED HEADACHE SCHO
  • 使用 R 中的 ggplot2 在分类散点图中添加水平线

    我正在尝试为 3 个组绘制一个简单的散点图 每个组具有不同的水平线 线段 例如 组 a 的 hline 为 3 组 b 的 hline 为 2 5 hline 为组 b c 组为 6 library ggplot2 df lt data f
  • 使用 R 从 Microsoft Outlook 发送电子邮件时的 Html 表输出格式

    我正在尝试使用以下方法将数据框转换为 html 表htmlTable打包 然后使用 Microsoft Outlook 作为电子邮件客户端发送电子邮件RDCOMClient通过附加 html 表作为电子邮件正文来进行打包 我是 HTML 编
  • R data.frame 从另一个变量选择的变量中获取值,向量化

    我收到的数据包含许多类似的变量 还有一个附加变量指示哪些变量one我真正想要的那些类似的变量 使用循环我可以查找正确的值 但是数据很大 循环很慢 而且看起来这应该是可矢量化的 我只是还没弄清楚怎么做 编辑 所选变量将用作同一数据框中的新变量
  • R/ggplot2:在执行 ylim 上限的同时平滑整个数据集

    更新 我找到了答案 包含在下面 我有一个包含以下变量和类似值的数据集 COBSDATE CITY RESPONSE TIME 2011 11 23 A 1 1 2011 11 23 A 1 5 2011 11 23 A 1 2 2011 1
  • 将复数名词转换为单数名词

    如何使用 R 将复数名词转换为单数名词 我使用 tagPOS 函数来标记每个文本 然后提取所有标记为 NNS 的复数名词 但是如果我想将这些复数名词转换为单数该怎么办 library openNLP library tm acq o lt
  • R data.table %like% 带有逻辑 AND

    我正在尝试构建一个闪亮的搜索引擎应用程序 我根据搜索关键字返回 data table DT lt data table field c A B C A C D A D A B A D B C F B D K DT field like A
  • 在 Linux 集群上安装 R `forecast` 包:编译器问题?

    我正在寻找测试性能R 更具体地说是一些例程forecast封装在具有 Intel Xeon Phi 协处理器的 HPC 集群上 据我所知 系统管理员已经建立了R 3 2 5按照英特尔网站上的说明从来源获取 https software in
  • 使用 dplyr 按行用以前的值填充缺失值

    我正在使用 R 中的一个数据框 该数据框跨行有一些缺失值 数据框是下一个 dput添加到最后 df id V1 V2 V3 V4 1 01 1 1 1 NA 2 02 2 1 NA NA 3 03 3 1 NA NA 4 04 4 1 2
  • 散点图的连续分位数

    我有一个数据集 我为其绘制了回归图 使用ggplot2 s stat smooth ggplot data mydf aes x time y pdm geom point stat smooth col red 我还想使用相同的方法获得分
  • 映射多个参数,其中一个参数是常量(数据)

    我正在努力在我构建的函数上使用 mapply 因为我在一个更大的环境中编程 所以我需要一个或多个参数 例如 如果我编写一个函数 其中一个参数是data fun test lt function data col val1 val2 retu

随机推荐

  • Python pickle 不是一对一的:不同的 pickle 给出相同的对象

    有人可以解释一下吗 pickle loads b x80 x03X x01 x00 x00 x00 q x00h x00 x86q x01 pickle loads b x80 x03X x01 x00 x00 x00 q x00X x01
  • Android 猴子跑步脚本

    我正在尝试通过 Monkey runner 命令提示符执行示例 python 程序 但它抛出错误 Can t open specified script file Usage monkeyrunner options SCRIPT FILE
  • 如何解析 WordOpenXML 输出中的 mathML?

    我只想读取用于生成方程的 xml 这是我通过使用获得的Paragraph Range WordOpenXML 但用于方程的部分并不符合MathML我发现Equation微软的MathML 我是否需要使用一些特殊的转换器来获取所需的 xml
  • For 循环有效,但 For Each 无效。为什么?

    在包含 forEach 循环的行上抛出了越界异常 但据我所知 这段代码没有任何问题 for 循环从 char 数组的元素 0 开始并循环直到到达最后一个元素 但是当我使用更长的 for 循环尝试此代码时 即 for int i 0 i lt
  • 更改 maven pom.xml 中传递依赖项的版本

    我一直在尝试覆盖我的一个项目中的传递依赖版本 我在 github 上找到了以下示例项目来进行实验 https github com Richou swagger codegen maven plugin https github com R
  • 求 2 次幂的算法

    我找到了一个小算法来确定一个数字是否是 2 的幂 但没有解释它是如何工作的 到底发生了什么 var potence n gt n n n 1 for var i 2 i lt 16 i if potence i console log i
  • Android ACTION_IMAGE_CAPTURE 与内存中的 EXTRA_OUTPUT

    当我打电话时用相机拍照时 File file new File getFilesDir getAbsolutePath myImage jpg Uri outputFileUri Uri fromFile file cameraIntent
  • $elemMatch 的 MongoDB 索引

    索引帮助页面位于http www mongodb org display DOCS Indexes http www mongodb org display DOCS Indexes没有提到 elemMatch 因为它说要在我的 2M 对象
  • iOS 11:大标题的 UINavigationBar 高度(模仿 Apple Music 应用)

    我试图模仿UINavigationBar由 Apple Music 应用程序使用 日期显示在大标题上方 我知道 Apple Music 应用程序不使用该标准UINavigationBar of ios11 questions tagged
  • 数据框中值之间的距离

    我有一个数据框 其中包含一系列虚拟变量 这些变量指示在另一个事件 a 之前发生的事件类型 e1 e2 我需要知道从类型 e1 和 e2 的每个事件到下一个事件 a 的索引值的距离 我尝试使用显示的数据进行演示 我已经研究了一些解决方案 包括
  • 管道和流程管理

    我正在开发一个用 C 实现的小型 shell tsh 这是一项作业 作业的一部分属于 PIPING 我必须将一个命令的输出通过管道传输到另一个命令 例如 ls l sort 当我运行 shell 时 我在其上执行的每个命令都由它生成的子进程
  • 没有河流的世界地图与 matplotlib / 底图?

    有没有一种方法可以用底图 或者没有底图 如果有其他方法的话 绘制大陆的边界 而不会出现那些烦人的河流 尤其是那段刚果河 连入海口都没有 令人不安 编辑 我打算进一步在地图上绘制数据 就像在底图库 http matplotlib org ba
  • 是否存在 Yahoogroups api?

    我们有 yahoogroups api 吗 我有一个 yahoo 群组 我希望每当我的 RSS 源更新时 都会自动向该群组发布一条消息 其中包含该帖子的链接 那可能吗 不 Yahoo 没有公共 API 团体 如果这样的 API 被开放 雅虎
  • Yii CDBCommand getText 显示 SQL 中的所有变量

    我正在使用 Yii 的 Yii app gt db gt createCommand 来构建 SQL 查询 为了查看 Yii 生成的 SQL 代码 我使用 CDBCommand 的 getText 方法 问题是 当我在包含参数的 SQL 代
  • 如何追踪僵尸对象崩溃?

    我的 iOS 应用程序发生了一些有线崩溃 并且它不是 100 可重现的 从崩溃日志中我可以看到无法识别的选择器 属性访问器 被发送到类型不正确的对象 很可能是僵尸 无论如何 XCode 中是否可以用来查看我是否正在尝试访问僵尸对象 Than
  • 如何存储(和使用)当前鼠标位置?

    存储当前鼠标位置 系统范围 然后 稍后 将鼠标放在该存储点的最佳方法是什么 NSEvent mouseLocation http developer apple com mac library documentation Cocoa Ref
  • Jsoup 解析和嵌套标签

    我正在学习 Jsoup 并有这个 HTML p Content p p Content p p Content p 我使用 Jsoup parse 和文档 select p 来捕获 内容 并且效果很好 但 p Content p p Con
  • Python向图像添加额外区域

    所以我有一张包含图像尺寸的表格 有多个不同尺寸的图像 66x66 400x400 等 我有一个图像示例 原始图像 其尺寸始终为 600x532 该图像上是一种产品 电视 PC 等 我必须调整该图像的大小 这不是问题 但如果我按照比例这样做
  • Linux bash 中波浪号的含义(不是主目录)

    首先 我知道 是主目录 CD 至 or 带我到主目录 然而 cd X带我去一个特别的地方 在那里X似乎是什么 在 bash 中 如果我点击 cd 然后点击选项卡 它会显示一堆可能的 X选项如 mail and postgres and ss
  • 如何计算r中两年的移动平均值

    我有一个关于并购 M As 的大数据框 900k 行 df 有四列 date 并购完成时 目标国家 被合并 收购的国家的公司 收购方 国家 收购方是哪个国家的公司 以及big corp 无论收购方是否是大公司 其中 TRUE 表示该公司很大