banner



How To Upload Data Into R

In this commodity, we are going to see how to Import information in R Programming Language.

Importing Data in R

Showtime, permit's consider a data-set up which we can apply for the demonstration. For this demonstration, we will use two examples of a unmarried dataset, one in .csv class and another .txt

Reading a Comma-Separated Value(CSV) File

Method one: Using read.csv() Part Read CSV Files into R

The function has two parameters:

  • file.choose(): Information technology opens a card to choose a csv file from the desktop.
  • header: Information technology is to indicate whether the starting time row of the dataset is a variable name or not. Utilise T/True if the variable name is present else put F/Imitation.

Case:

R

data1 <- read.csv ( file.choose (), header=T)

data1

Output:

Method 2: Using read.table() Function

This function specifies how the dataset is separated, in this case we take sep=", " as an argument.

Example:

R

data2 <- read.table ( file.choose (), header=T, sep= ", " )

data2

Output:

Reading a Tab-Delimited(txt) File in R Programming Language

Method 1: Using read.delim() Office

The role has 2 parameters:

  • file.choose(): It opens a menu to choose a csv file from the desktop.
  • header: Information technology is to indicate whether the first row of the dataset is a variable name or non. Apply T/Truthful if the variable name is present else put F/Fake.

Example:

R

data3 <- read.delim ( file.choose (), header=T)

data3

Output:

Method 2: Using read.table() Function

This function specifies how the dataset is separated, in this instance nosotros take sep="\t" as the argument.

Example:

R

data4 <- read.tabular array ( file.choose (), header=T, sep= "\t" )

data4

Output:

Using R-Studio

Here nosotros are going to import data through R studio with the following steps.

Steps:

  • From the Environment tab click on the Import Dataset Card
  • Select the file extension from the pick
  • In the 3rd step, a pop-upwards box will appear, either enter the file name or browse the desktop.
  • The selected file will be displayed on a new window with its dimensions.
  • In order to see the output on the console, type the filename.

Example:

R

Output:

  • In guild to load the data onto the panel for use, we use the adhere control.

Case:

R

Read JSON Files Into R

In order to work with JSON files in R, i needs to install the "rjson"  package. The nearly common tasks done using JSON files nether rjson packages are as follows:

  • Install and load the rjson package in R console
  • Create a JSON file
  • Reading data from JSON file
  • Write into JSON file
  • Converting the JSON data into Dataframes
  • Working with URLs

JSON file for demonstration:

{     "ID":["ane","2","3","4","5"],    "Name":["Mithuna","Tanushree","Parnasha","Arjun","Pankaj"],    "Salary":["722.5","815.2","1611","2829","843.25"],    "StartDate":["half-dozen/17/2014","1/one/2012","11/15/2014","9/23/2013","5/21/2013"],    "Dept":["Information technology","It","Hr","Operations","Finance"] }

Code:

R

library ( "rjson" )

result <- fromJSON (file = "E:\\example.json" )

print (result)

Output:

$ID [i] "i" "2" "3" "four" "5"  $Name [1] "Mithuna"   "Tanushree" "Parnasha"  "Arjun"     "Pankaj"  $Salary [ane] "722.5"  "815.2"  "1611"   "2829"   "843.25"  $StartDate [ane] "half-dozen/17/2014"  "1/one/2012"   "11/xv/2014" "9/23/2013"  "5/21/2013"  $Dept [1] "It"         "It"         "60 minutes"         "Operations" "Finance"

Source: https://www.geeksforgeeks.org/importing-data-in-r-script/

Posted by: neffhuselan.blogspot.com

0 Response to "How To Upload Data Into R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel