如果一个向量" x"是一个向量df $ y的子集,df是一个由以下组成的数据帧:" y"和频率" fy",我可以获得" x"和" fx" ? [R]

时间:2017-11-23 21:26:15

标签: r dataframe

如果list_adf$list_b的子集,其中df$frequency_bdf$list_b中值的频率,我想创建一个我想要的数据框list_a具有相应的频率,因此是数据帧[df1$list_a,df1$frequency_a]。 (注意:list_a中的所有元素都在df$list_b中。

示例:

list_a <- c("John","George","Jack","Kathrine")
df$list_b <- c("Mario","Jack","Ana","George","Loizos",
          "Kathrine","John","Jack","Yannis")

其中,

df$frequency_b <- c("10","3","15","23","13","50","553","334","332")

我想要一个数据框,例如:

df1$list_a <- c("John","George","Jack","Kathrine")

和相应的频率:

df1$frequencies <- c(553,3,15,23)

有没有办法在R中实现这个?

1 个答案:

答案 0 :(得分:0)

可以使用%in%运算符对原始数据框中的名称进行子集化。

> list_a <- c("John","George","Jack","Kathrine")
> list_b <- c("Mario","Jack","Ana","George","Loizos",
+                "Kathrine","John","Jack","Yannis")
> frequency_b <- c("10","3","15","23","13","50","553","334","332")
> df <- data.frame(name=list_b, count=frequency_b)
> df1 <- df[df$name %in% list_a,]
> df1
      name count
2     Jack     3
4   George    23
6 Kathrine    50
7     John   553
8     Jack   334

请注意,您的数据中有两个名为Jack的人,因此输出数据框有5行,而不是原始帖子中的4行。

的问候,

Len