【问题标题】:Changing Observations Based on Filters基于过滤器改变观察
【发布时间】:2020-12-12 21:24:26
【问题描述】:

我有一个数据框,其中包含两列:clean.data$bilateralclean.data$if.bilateral.other.party。在双边数据中,有三个观察值:YNBilateral(是的,我知道观察值与列名基本相同,这是不好的。观察值大写,而列名不是)。

clean.data <- data.frame("bilateral" = c("Y", "Bilateral", "N", "Y", "Bilateral", "N"),
       "if.bilateral.other.party" = c("Jordan", "Sweeden", NA, "Uk,Netherlands", "Russia,Poland", "NewZealand"))

BilateralY 条约在if.bilateral.other.party 中应该只有一项观察,但有些则没有。例如,Uk,Netherlands 不应列为Bilateral,而应列为Nif.bilateral.other.party 列中的空格我已经去掉了,各方之间有逗号。

我正在尝试识别当前不应该标记为 BilateralY 的观察结果,并在这种情况下将观察结果更改为 N。我还需要做相反的事情,将观察N 更改为Y,如果他们列出了其他方。

我该怎么做?

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。
  • 我加了一个,很抱歉

标签: r dplyr tidyr


【解决方案1】:

在没有示例数据集的情况下,此解决方案基于推测以及您如何描述您想要完成的操作。

library("dplyr")
library("stringr")
library("purrr")

clean.data <- tribble(
  ~bilateral, ~if.bilateral.other.party,
  "Y", "UK,Netherlands",
  "Bilateral", "Sweden,France",
  "N", "Germany,UK"
)
clean.data
#> # A tibble: 3 x 2
#>   bilateral if.bilateral.other.party
#>   <chr>     <chr>                   
#> 1 Y         UK,Netherlands          
#> 2 Bilateral Sweden,France           
#> 3 N         Germany,UK

# Split and count countries and assign new bilateral column
clean.data %>%
  mutate(list_countries = str_split(if.bilateral.other.party, ",")) %>%
  mutate(num_countries = map_int(list_countries, function(x) { length(x) })) %>%
  mutate(new_bilateral = case_when(
    num_countries > 1 & bilateral %in% c("Y", "Bilateral") ~ "N",
    num_countries > 1 & bilateral == "N" ~ "Y",
    TRUE ~ bilateral
  ))
#> # A tibble: 3 x 5
#>   bilateral if.bilateral.other.party list_countries num_countries new_bilateral
#>   <chr>     <chr>                    <list>                 <int> <chr>        
#> 1 Y         UK,Netherlands           <chr [2]>                  2 N            
#> 2 Bilateral Sweden,France            <chr [2]>                  2 N            
#> 3 N         Germany,UK               <chr [2]>                  2 Y

reprex package (v0.3.0) 于 2020 年 12 月 12 日创建

以下是使用您提供的样本数据得出的结果。

clean.data <- data.frame(
  "bilateral" = c("Y", "Bilateral", "N", "Y", "Y", "N"),
  "if.bilateral.other.party" = c("Jordan", "Sweeden", NA, "Uk,Netherlands", "Russia,Poland", "NewZealand"), 
  stringsAsFactors = FALSE)

clean.data %>%
  mutate(list_countries = str_split(if.bilateral.other.party, ",")) %>%
  mutate(num_countries = map_int(list_countries, function(x) { length(x) })) %>%
  mutate(new_bilateral = case_when(
    num_countries > 1 & bilateral %in% c("Y", "Bilateral") ~ "N",
    num_countries > 1 & bilateral == "N" ~ "Y",
    TRUE ~ bilateral
  ))
#>   bilateral if.bilateral.other.party  list_countries num_countries
#> 1         Y                   Jordan          Jordan             1
#> 2 Bilateral                  Sweeden         Sweeden             1
#> 3         N                     <NA>              NA             1
#> 4         Y           Uk,Netherlands Uk, Netherlands             2
#> 5         Y            Russia,Poland  Russia, Poland             2
#> 6         N               NewZealand      NewZealand             1
#>   new_bilateral
#> 1             Y
#> 2     Bilateral
#> 3             N
#> 4             N
#> 5             N
#> 6             N

reprex package (v0.3.0) 于 2020-12-12 创建

【讨论】:

  • 我添加了一个例子。对于那个很抱歉。根据示例,这仍然正确吗?
  • 这似乎仍然适用于您添加的示例。
  • 我已经使用您提供的示例数据添加了数据处理结果。为避免重写您的原始数据,我创建了一个带有新更改的新变量 new_bilateral
猜你喜欢
  • 1970-01-01
  • 2016-11-19
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多