Calculating the number and frequency of list items in R?

I have a long list containing different numbers of elements that can also be repeated in the same field. This is an example of the first five lines:

A <- list(c("JAMES","CHARLES","JAMES","RICHARD"), c("JOHN","ROBERT","CHARLES"), c("CHARLES","WILLIAM","CHARLES","MICHAEL","WILLIAM","DAVID","CHARLES","WILLIAM"), c("CHARLES"), c("CHARLES","CHARLES")) 

Now I would like to calculate the number of elements for each line of the list.
My desired result would look something like this:

 [1] 4 [2] 3 [3] 7 [4] 1 [5] 2 

In addition to the fact that I would like to know the frequency, the term "CHARLES" appears on every line.
Based on my example, I would like to get the same result:

 [1] 1 [2] 1 [3] 3 [4] 1 [5] 2 

I thought about this:

 > table(A) Error in table(A) : all arguments arguments must have same length > sum(A) Error in sum(A) : invalid 'type' (list) of argument 

But I do not know what to do with these error messages, and, unfortunately, I do not know the alternatives.
I know the number of lines in a list:

 > length(A) [1] 5 

But this, unfortunately, does not answer my question. I could not find any other answers.
Therefore, I would like to ask you, please, help me calculate these two measures!

Thank you in advance!

+4
source share
3 answers

You should check out lapply and sapply to sapply over the lists:

 sapply(A, length) [1] 4 3 8 1 2 sapply(A, function(x)sum(grepl("CHARLES", x))) [1] 1 1 3 1 2 

What grepl() does matches the regular expression in your text and returns TRUE or FALSE depending on whether a match exists. Then I do sum() on these booleans, i.e. Summing the TRUE values.

+6
source
 sapply(A, function(x) length(x)) [1] 4 3 8 1 2 un <- unique(unlist(A)) nm <- lapply(un, function(x) sapply(A, function(y) sum(y == x))) names(nm) <- un nm $JAMES [1] 2 0 0 0 0 $CHARLES [1] 1 1 3 1 2 $RICHARD [1] 1 0 0 0 0 $JOHN [1] 0 1 0 0 0 $ROBERT [1] 0 1 0 0 0 $WILLIAM [1] 0 0 3 0 0 $MICHAEL [1] 0 0 1 0 0 $DAVID [1] 0 0 1 0 0 
+6
source

To calculate the length of the elements of list A. You can use the following command:

 list.len <- lapply(1:length(A),function(x) length(A[[x]])) unlist(list.len) [1] 4 [2] 3 [3] 7 [4] 1 [5] 2 

to calculate the number of times “CHARLES” appears in each element of the list, you can do the following:

 len.name <- lapply(1:length(A),function(x) length(which(A[[x]] == "CHARLES"))) len.name [[1]] [1] 1 [[2]] [1] 1 [[3]] [1] 3 [[4]] [1] 1 [[5]] [1] 2 

then you can block

 unlist(len.name) 

and then you get the result:

 [1] 1 [2] 1 [3] 3 [4] 1 [5] 2 
+3
source

All Articles