R中的列操作-匹配正确的名称

时间:2018-10-21 14:44:29

标签: r

我有一个由多列和数千行组成的data.frame。下面,我尝试显示其(头):

|year           |state_name|idealPoint|   vote_no|  vote_yes| 
|:--------------|---------:|---------:|---------:|---------:|
|1971           |   China  |  -25.0000|   31.0000|   45.4209|
|1972           |   China  |  -26.2550|   38.2974|   45.4209|
|1973           |   China  |   28.2550|   35.2974|   45.4209|
|1994           |   Czech  |   27.2550|   34.2974|   45.4209|

如您所见。并非所有国家(其中有196个)都在同一年参加联合国投票。

我要做什么是要在data.frame(投票)中创建一个新列,其中包括ChinaIdealpoints与Czech Ideal点(给定年份...)之间的绝对差。我知道如何使用dplyr创建新列,但是如何从196个国家/地区列表中乘以正确的国家/地区? (我认为加入年份之间的差额可以手动删除)。

最终输出应为新的data.frame(或投票中的新列),如下所示: 1994年的中国理想点为2.2550

|year           |state_name|idealPoint|Abs.Difference China_Czech
|:--------------|---------:|---------:|-------------------------:|
|1971           |   China  |  -25.0000|                   NA     |
|1972           |   China  |  -26.2550|                   NA     |
|1973           |   China  |   28.2550|                   NA     |
|1994           |   Czech  |   27.2550|                  25.0000 |

2 个答案:

答案 0 :(得分:0)

这也许能解决您的问题吗?

library(tibble)
library(dplyr)

a <- tribble(
  ~year, ~ctry, ~vote,
  1994, "China", 5,
  1995, "China", 100,
  1996, "China", 600,
  1997, "China", 45,
  1998, "China", 9,
  1994, "Czech_Republic", 1,
  1995, "Czech_Republic", 5,
  1996, "Czech_Republic", 100,
  1997, "Czech_Republic", 40,
  1998, "Czech_Republic", 6,
)

a %>%
  group_by(year) %>%
  mutate(foo = abs(lag(lead(vote) - vote)))

输出:

# A tibble: 10 x 4
# Groups:   year [5]
    year ctry            vote   foo
   <dbl> <chr>          <dbl> <dbl>
 1  1994 China              5    NA
 2  1995 China            100    NA
 3  1996 China            600    NA
 4  1997 China             45    NA
 5  1998 China              9    NA
 6  1994 Czech_Republic     1     4
 7  1995 Czech_Republic     5    95
 8  1996 Czech_Republic   100   500
 9  1997 Czech_Republic    40     5
10  1998 Czech_Republic     6     3

您必须过滤数据以适合您的需求,例如按国家。

答案 1 :(得分:0)

代码:

df1 <- data.frame(year = c(1994,1995,1996,1997,1994,1995,1996,1997),
                  state_name = c("China","China","China","China","Czech_Republic","Czech_Republic","Czech_Republic","Czech_Republic"),
                  idealpoints = c(-25.0000,-26.2550,28.2550,27.2550,-27.0000,-28.2550,29.2550,22.2550),
                  vote_no = c(31.0000,38.2974,35.2974,34.2974,33.0000,36.2974,37.2974,38.2974),
                  vote_yes = c(45.4209,45.4209,45.4209,45.4209,45.4209,45.4209,45.4209,45.4209))

china_df <- df1[df1$state_name == "China",]
czech_df <- df1[df1$state_name == "Czech_Republic",]
china_czech_merge <- merge(china_df,czech_df,by = "year")

china_czech_merge$Abs_diff <- abs(china_czech_merge$idealpoints.x - china_czech_merge$idealpoints.y)

输出:

  year state_name.x idealpoints.x vote_no.x vote_yes.x   state_name.y idealpoints.y vote_no.y vote_yes.y Abs_diff
1 1994        China       -25.000   31.0000    45.4209 Czech_Republic       -27.000   33.0000    45.4209        2
2 1995        China       -26.255   38.2974    45.4209 Czech_Republic       -28.255   36.2974    45.4209        2
3 1996        China        28.255   35.2974    45.4209 Czech_Republic        29.255   37.2974    45.4209        1
4 1997        China        27.255   34.2974    45.4209 Czech_Republic        22.255   38.2974    45.4209        5

我认为这对您有用。

谢谢