如果list_a
是df$list_b
的子集,其中df$frequency_b
是df$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中实现这个?
答案 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