df <- data.frame( V1= 1, V2= c( 2, 3, 2, 1), V3= c( 1, 2, 1, 1))
dfO <- df[ do.call( order, df), ]
dfOD <- duplicated( dfO)
dfODTrigger <- ! c( dfOD[-1], FALSE)
dfOCounts <- diff( c( 0, which( dfODTrigger)))
cbind( dfO[ dfODTrigger, ], dfOCounts)
V1 V2 V3 dfOCounts
4 1 1 1 1
3 1 2
Have a look at the dplyr package
library(dplyr)
n <- 1000
data_frame(
V1 = sample(0:1, n, replace = TRUE),
V2 = sample(0:1, n, replace = TRUE),
V3 = sample(0:1, n, replace = TRUE)
) %>%
group_by(V1, V2, V3) %>%
mutate(
Freq = n()
)
ir. Thierry Onkelinx
Instituut voor natuur- en b
10, 2015 9:11 AM
> To: r-help@r-project.org
> Subject: [R] Counting occurrences of a set of values
>
> Can anyone suggest a way of counting how frequently sets of values occurs in a
> data frame? Like table() only with sets.
>
> So for a dataset:
>
> V1, V2, V3
> 1, 2
On 10/09/2015 9:11 AM, Thomas Chesney wrote:
> Can anyone suggest a way of counting how frequently sets of values occurs in
> a data frame? Like table() only with sets.
Do you want 1,2,1 to be the same as 1,1,2, or different? What about
1,2,2? For sets, those are all the same, but for most purp
Can anyone suggest a way of counting how frequently sets of values occurs in a
data frame? Like table() only with sets.
So for a dataset:
V1, V2, V3
1, 2, 1
1, 3, 2
1, 2, 1
1, 1, 1
The output would be something like:
1,2,1: 2
1,3,2: 1
1,1,1: 1
Thank you,
Thomas Chesney
This message and an
5 matches
Mail list logo