How can i check integer in R?

Hi guys!

Just curious how can i check if a column of an R data frame has values which are integers? I can’t seem to find any function to check.

Secondly, why for pivot_longer, we must end with names_transform to change the year to an integer? What difference does it make? This is an example:

table4 %>%
  pivot_longer(cols=c(`1999`,`2000`),names_to = "year", values_to = "cases",(names_transform = list(year = as.integer)))


1 Like

Hey @lauratangwt,

You can check the data type of the column by going class(dataframe_name$name_of_column). If it’s an integer, it’ll read as so.

As for your second question, it’s more of an issue of (a) your year info may be entered as a string instead of a numeric and (b) good practice if someone has to deal with date-based data. Say if I need to go back and see the x number of days from point A to point B or whatever, it makes it possible to work with the data immediately to get my answer instead of wasting time trying to figure out why the piece of code that you wrote that should’ve work didn’t.

1 Like