[R] counting unique values (summary stats)

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Mar 21 23:54:32 CET 2019


On 3/21/19 3:31 PM, reichmanj using sbcglobal.net wrote:
> r-help
>
> I have the following little scrip to create a df of summary stats.  I'm
> having problems obtaining the # of unique values
>
>             unique=sapply(myData, function (x)
>               length(unique(x), replace = TRUE))

I just looked up the usage on `length` and do not see any possibility of 
using a "replace" parameter. It's also unclear what sort of data object 
`myData` might be. (And you might consider using column names other than 
the names of R functions.)


-- 

David.

>
> Can I do that, or am I using the wrong R function?
>
> summary.stats <- data.frame(mean=sapply(myData, mean, na.rm=TRUE),
>             sd=sapply(myData, sd, na.rm=TRUE),
>             min=sapply(myData, min, na.rm=TRUE),
>             max=sapply(myData, max, na.rm=TRUE),
>             median=sapply(myData, median, na.rm=TRUE),
>             length=sapply(myData, length),
>             unique=sapply(myData, function (x)
>               length(unique(x), replace = TRUE))
>             miss.val=sapply(myData, function(y)
>               sum(length(which(is.na(y))))))
>
> Jeff Reichman
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list