Tuesday, November 6, 2018

How to read the data from a User input in R Programming

Read the data from a User input in R Programming
Usually you will obtain a data frame by importing it from CSV,EXCEL,SAS, SPSS, Stata, a database, or an ASCII file. To create it interactively, you can do something like the following.
# create a data frame from scratch by defining the variables with values
> age <- c(25, 30, 56)
> gender <- c("male", "female", "male")
> weight <- c(160, 110, 220)
> mydata <- data.frame(age,gender,weight)
> mydata
age gender weight
1 25 male 160
2 30 female 110
3 56 male 220

#We can also use R's built in spreadsheet to enter the data interactively, as in the following example.
# define a data frame with empty variables
mydata <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0))

# enter data using editor 
mydata <- edit(mydata)

now a spreadsheet will open as follows to enter the data.Enter data in the popup spreadsheet for the required variables and ignore the other default variables.

> mydata
age gender weight
1 27 male 75
2 32 female 65
3 35 male 80

# note that without the assignment in the line above,"mydata <- edit(mydata)" the edits are not saved to the dataframe mydata.

# if you wants to edit the header name, simply double click on it and then rename it. you can also choose the data type as numeric or character from the same window as follows.



 #Finally, once your data entry and changes done, simply close the window, so that the data will be saved to the dataframe.

Reading data from a Clipboard, Copied from Excel :
We can read the data to R from a Clipboard which is copied from and excel spread sheet as follows in the Windows environment.
First copy the data from spread sheet


Next run the following Code
> mydata <- read.delim("clipboard")
> mydata
  SalesOrder_Id Order_Date FiscalQuarter      Cust_Name Net_Sales
1     123456798  2/13/2014       Q1-2014    XYZ USA Inc     955.5
2     123456792  2/14/2014       Q1-2014    XYZ USA Inc  932750.0
3     123456799  3/16/2014       Q1-2014 DEF Canada Ltd  817000.0
4     123456793  3/20/2014       Q1-2014 DEF Canada Ltd  414050.0
5     123456796   6/5/2014       Q2-2014    XYZ USA Inc 1171800.0
6     123456797   7/4/2014       Q3-2014 DEF Canada Ltd  145200.0
7     123456794  10/6/2014       Q4-2014    XYZ USA Inc    9660.0
8     123456795  11/9/2014       Q4-2014 DEF Canada Ltd    3240.0

we can also use following R Code..
mydata<-read.table(file = "clipboard", sep = "\t", header=TRUE)

> mydata 
  SalesOrder_Id Order_Date FiscalQuarter      Cust_Name Net_Sales
1     123456798  2/13/2014       Q1-2014    XYZ USA Inc     955.5
2     123456792  2/14/2014       Q1-2014    XYZ USA Inc  932750.0
3     123456799  3/16/2014       Q1-2014 DEF Canada Ltd  817000.0
4     123456793  3/20/2014       Q1-2014 DEF Canada Ltd  414050.0
5     123456796   6/5/2014       Q2-2014    XYZ USA Inc 1171800.0
6     123456797   7/4/2014       Q3-2014 DEF Canada Ltd  145200.0
7     123456794  10/6/2014       Q4-2014    XYZ USA Inc    9660.0
8     123456795  11/9/2014       Q4-2014 DEF Canada Ltd    3240.0

for Mac : (not tested )
> mydata <- read.delim(pipe(“pbpaste”))

Reading a user Input with Function ( Integer ) :
>readinteger <- function()
{
  n <- readline(prompt="Enter an integer: ")
  return(as.integer(n))
}

print(readinteger())

Enter an integer: 15
[1] 15

readline() : lets the user enter a one-line string at the terminal.
prompt : This argument is printed in front of the user input, usually ends with ":"
as.integer : Converts the number format to integer.

Preventing failure if no number is entered
Right now if the user doesn't enter an integer, as.integer will return NA (Not Available). We can avoid this by using is.na to check the user input and asking again if the value is NA:
>readinteger <- function()
{
  n <- readline(prompt="Enter an integer: ")
  n <- as.integer(n)
  if (is.na(n)){
    n <- readinteger()
  }
  return(n)
}
print(readinteger())

Enter an integer: abc
Enter an integer: 45
[1] 45

Warning message:
In readinteger() : NAs introduced by coercion

However, a warning message is still shown. We can avoid it using the !grepl, which returns TRUE if the regular expression "^[0-9]+$" is matched. (The expression checks for a string that consists of nothing but one or more digits.)
! negates the result and the if branch is executed if the user-entered a string instead of an integer.
>readinteger <- function()
{
  n <- readline(prompt="Enter an integer: ")
  if(!grepl("^[0-9]+$",n))
  {
    return(readinteger())
  }
 
  return(as.integer(n))
}
print(readinteger())
Enter an integer: abc
Enter an integer: 25
[1] 25


--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
-------------------------------------------------------------------------------------------------------- 

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog