简体   繁体   中英

R table not outputting results within for loop

I am just trying to loop over my columns and print out the count of unique values for further processing - but getting not output. This should be simple but I am not getting any output. Here is a simplified version of my code. Is there something glaringly obviously missing as I suspect

for (i in 1:length(mydata)) {
 (table(mydata[,i]))
}

Do you mean using apply ?

> x <- data.frame("SN" = 1:4, "Age" = c(21,15,56,15), "Name" = 
c("John","Dora","John","Dora"))
> apply(x,2,function(x) unique(x))
$SN
[1] "1" "2" "3" "4"

$Age
[1] "21" "15" "56"

$Name
[1] "John" "Dora"

You can also count the uniques like this:

> apply(x,2,function(x) length(unique(x)))
  SN  Age Name 
   4    3    2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM