How to do Data Format in R

Data Format in R, You’ll learn about data formats and why reformatting data can help you enhance your data analysis in this tutorial.

Data is typically acquired from a variety of sources and by a variety of persons, and it is kept in a variety of formats.

Data formatting is the process of transforming data into a standardized format that allows you to make meaningful comparisons.

Data formatting is an important aspect of dataset cleansing since it guarantees that data is consistent and easy to understand.

Let’s take an example of data set containing Cities, Bangalore, Bengaluru, Bnglr all are the different expressions be used to symbolize Bangalore City.

In the majority of cases, you’ll want to consider them all as a single unit, or format, to make statistical analysis easier later on.

Customer Segmentation K Means Cluster »

Data Format in R

As discussed in one of our old posts, the same dataset will utilize here also.

library(tidyverse)
library(dplyr)
library(ggplot2)
data<-read.csv("D:/RStudio/Airlinedata.csv",1)
head(data)

There is a column called “FlightDate” in the Airline dataset. The “FlightDate” field is formatted as “year-month-day,” with 2003 as the year, 03 as March, and 28 as the day.

The “FlightDate” field can be separated into three columns: “year,” “month,” and “day.”

Reformatting the date in tidyverse is as simple as typing one line of code. You can do the same while utilizing different packages also but here we are concentrating only on tidyverse package.

Because one of our old posts discussed the important “packages for data science” contains tidyverse.

Cluster Meaning-Cluster or area sampling in a nutshell »

This example reformats the column with the separate() function, separating the date and renaming the three new columns “year,” “month,” and “date.”

data1<-data %>% separate (FlightDate,sep="-", into=c("year","month", "day"))
head(data1)

The data type may be wrongly determined for a variety of reasons, including when importing a dataset into R or processing a variable.

For example, the allocated data type for the flight date is “character,” despite the fact that the desired data type is numeric.

str(data1)

It’s critical to investigate the column’s data type and convert it to the correct data type for further analysis; otherwise, the models you later construct may act strangely, and valid data may be interpreted as missing data.

KNN Algorithm Machine Learning » Classification & Regression »

The sapply() function in R can be used to verify the data type of each column in a dataset to determine column data types.

sapply(data1,typeof)

If this gives the wrong conversion then you can make use of mutate function.

data2<-data1 %>%
select(year, month, day) %>%
mutate_all(type.convert) %>%
mutate_if(is.character,as.numeric)
str(data2)

You learned in this tutorial that reformatting data is a method of bringing information into a common standard of expression, which allows you to make meaningful comparisons.

Principal component analysis (PCA) in R »

You may also like...

2 Responses

  1. Don MacQueen says:

    In this case, the Base R approach might be, for example

    ## example data
    > dt df df
    year mon day
    1 2021 1 14
    2 2022 2 7

    > str(df)
    ‘data.frame’: 2 obs. of 3 variables:
    $ year: num 2021 2022
    $ mon : num 1 2
    $ day : num 14 7

    ##
    (or use as.integer() instead)

    ##
    Admittedly, the Base R one-liner is arcane. And would probably break badly if any of the dates were malformed.

    Another approach, not as elegant, but easier to understand by someone reading the code five years from now, could be built around this:

    ## calculate numeric years from the character formatted dates:
    > as.numeric(format( as.Date(dt) , ‘%Y’))
    [1] 2021 2022

    ##
    in general, I would not use a one-liner in any approach. I’d separate the steps, because QA is much easier when intermediate objects are available for inspection.

    ##
    On a different topic, formatting in R means, to me, controlling how an object is displayed. For example,

    > foo format(foo)
    [1] “1234.23”

    > format(foo, digits=18)
    [1] “1234.23000000000002”

    > format(foo, big.mark=’,’)
    [1] “1,234.23”

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × five =