从多个向量或列表中查找字符的出现

时间:2017-03-14 15:03:10

标签: r unique combinations frequency

我希望找到在多个向量或列表中出现唯一/不同字符的次数。

也许最好在一个例子中描述;

在这个例子中,假设“唯一字符”是字母。而多元化的“载体”就是书。我希望发现这些字母的出现随着图书数量的增加而增加。

# Initial data in the format of a list
book_list <- list(book_A <- c("a", "b", "c", "z"),
                  book_B <- c("c", "d", "a"),
                  book_C <- c("b", "a", "c", "e", "x"))

# Initial data in the format of multiple vectors
book_A <- c("a", "b", "c", "z")
book_B <- c("c", "d", "a")
book_C <- c("b", "a", "c", "e", "x")

# Finding the unique letters in each book
# This is the part im struggling to code in a loop fashion
one_book <- length(unique(book_A))
two_book <- length(unique(c(book_A, book_B)))
three_book <- length(unique(c(book_A, book_B, book_C)))

# Plot the desired output
plot(x=c(1,2,3), 
     y=c(one_book, two_book, three_book), 
     ylab = "Number of unqiue letters", xlab = "Book Number",
     main="The occurence of unique letters as number of book increases")

enter image description here

要注意:实际数据集要大得多。每个向量(book_A,book_B等)的长度约为7000。

我试图用dplyr或数据框解决问题,但我还没到那里。

# Explore data frame option with an example data
library(dplyr)
df <- read.delim("http://m.uploadedit.com/ba3s/148950223626.txt")

# Group them
df_group <- dplyr::group_by(df, book) %>% summarize(occurence = length(letter))

# Use the cummuative sum
plot(x=1:length(unique(df$book)), y=cumsum(df_group$occurence))

但是我知道情节不正确,因为它只绘制累计和而不是我想要的。任何提示都会有所帮助。

为了增加复杂性,如果可以绘制具有最短字母数量的书,那将是很好的。沿线的东西

# Example ;
# Find the length of the letters in the book
lapply(book_list, length)

# I know that book_B is has the shortest number of letters (3);
# followed by book_A (4) then book_C (5)
one_book <- length(unique(book_B))
two_book <- length(unique(c(book_B, book_A)))
three_book <- length(unique(c(book_B, book_A, book_C)))


plot(x=c(1,2,3), 
     y=c(one_book, two_book, three_book), 
     ylab = "Number of letters", xlab = "Book Number")

1 个答案:

答案 0 :(得分:1)

您可以将Reduceaccumulate = TRUE一起使用,即

sapply(Reduce(c, book_list, accumulate = TRUE), function(i) length(unique(i)))
#[1] 4 5 7