Rime
Rime

Reputation: 942

Character to numeric summary failure

I have a data frame that contains 3 sets of numbers that I am trying to convert to numeric to do analysis on it.

For example:

data <- c("453", "021", "203", "675", "919", "138", "645", "774", "309", 
"351", "129", "428", "435", "704", "725", "530", "952", "008", 
"875", "016", "816", "047", "024", "186", "560", "165", "395", 
"969", "459", "213", "932", "987", "212", "441", "242", "930", 
"319", "327", "064", "194", "453", "129", "680", "998", "208", 
"377", "880", "498", "789", "460", "578", "167", "843", "421", 
"306", "429", "540", "744", "575", "423", "546")

The problem is that some of the numbers start with 0 so I can't turn them into numeric because if I do I will lose the "0".

Is there a way to work around this so I can use summary(data) to get an accurate summary of the data?

Upvotes: 2

Views: 34

Answers (1)

akrun
akrun

Reputation: 887118

We need to use the correct method for summary

summary.factor(data)

By default, it requires a factor or numeric class to return the frequency count. If it is character, it returns the length, class and mode. So, either convert it to factor to select the appropriate method by summary or explicitly call summary.factor


A better option would be table which works on all of the above classes

table(data)

Upvotes: 3

Related Questions