Sunday, August 13, 2017

(R) Central Tendency

In this article, I will be discussing R's ability to perform the various methods that are necessary to determine central tendency. I will be using two data vectors as examples to generate the values discussed within this entry. To follow along, please create the following vectors by utilizing the following lines of code:

x <-c(53,46,61,97,44,87,40,15,29,99,85,98,17,3,46,25,15,19,2,32,67,34,39,100,88,40,40,87,89,86,69,67,89,84,98,43,75,66,40,76,48,82,45,99,10,59,15,13,99,45,78,66,59,26,2,91,80,42,94,12,9,24,37,14,18,86,35,96,56,50,22,39,58,82,11,56,50,30,99,64,74,13,14,7,5,97,59,91,57,69,58,36,43,77,36,2,58,86,89)

y <- c(1,1,1,2,2,2,3)


summary()

Summary is a useful R function, in that it provides the user with console output pertaining to the value that was initially passed to it.

Summary will print to the console:

Min (the smallest value within the set)
1st Qu. (the value of the first quartile)
Median (the median value)
Mean (the mean value)
3rd Qu. (the value of the third quartile)
Max (the max value)

If were to utilize this function while passing to it the value of 'x', the following information would be generated and printed to the R console window:

summary(x)

Min. 1st Qu. Median Mean 3rd Qu. Max.
2.00  29.50   53.00    53.15 82.00    100.00

If you wanted to generate each value independently, you could use the following functions:

mean()

For the mean value.

median()

For the median value.

range()

For the lowest and highest values.

Finding the Mode

Unfortunately, R does not have a standard function contained within its library that can be utilized to generate the mode. However, after careful searching, I found a very good substitute. The code is below. This code was taken from a YouTube user named, economicurtis. It was featured in his video: "Calculating Mode with R Software (More on R's Summary Stats". A link exists to this video at the end of the article.*

temp <- table(as.vector(<vectorname>))
names(temp)[temp == max(temp)]

The first line creates a new table for the data vector, and the second line generates the value. If the data is bi-modial, two values will be generated. Here is an example of the code with 'y' being utilized as the vector value.

temp <- table(as.vector(y))
names(temp)[temp == max(temp)]

Since 'y' is bi-modial, the output that is printed to the console window should be:

[1] "1" "2"

Finding the Variance

To derive the variance from a vector, the following function can be utilizied:

var()

Finding the Standard Deviation

This funciton can be used to derive the standard deviation from a vector:

sd()

Tukey's Five Number Summary

This function provides sample percentiles, which can be useful in descriptive statistics:

fivenum()


For example, if were to use this function on x:

fivenum(x)

The following information would be printed to the console window:

[1] 2.0 29.5 53.0 82.0 100.0

(2.0) The first value is the smallest observation.
(29.5) The second value is the value of the first quartile.
(53.0) The third value is the median.
(82.0) The fourth value is the value of the third quartile.
(100.0) And the final value is largest obervation.

Interquartile Range


The interquartile range, or IQR, is the value between the third and first quartiles. This value can be derived with the following function.

IQR()

In the next article, we will begin graphing box plots, and histograms.

* - https://www.youtube.com/watch?v=YvdYwC2YgeI

Thursday, August 10, 2017

(R) Misc.

Before I begin writing entries pertaining to data modeling, there are a few final concepts that I would like to review. In this article, I will be discussing different methodologies that were not included in my previous entries. These concepts are nevertheless important, and should be mentioned before progressing on to more difficult tasks.

Saving Work

When exiting R-Studio, you will be presented with a few options pertaining to saving data form the current session. The first prompt will ask if you want to save your current R script file. This file has an .R extension, and contains the code that you created during your session.

Upon re-starting R Studio, you should notice that all of the objects and scripts that you were previously working on, have been loaded into the platform. This occurs due to the R workspace image file. R workspace files are automatically loaded into R Studio, and contain information pertaining to your prior session. This file can be located within your R working directory, and has the assigned name ".RData".

If you wanted to manually create an .RData file with a unique name from the R console, you could utilize the command:

save.image("<filename>.rdata") 


.Rhistory is another R-Studio file. This file contains the console log from the previous session. This file can be opened and viewed with WordPad or other text editing software.

To exit R from the command line, the following command can be used:

q()


Saving Data

If you would like to save one of the data sets that you have recently edited as an R Data Frame, this can be achieved with the following line of code:

save(<dataframename>, file=<filepathway>.rda”)

Example:

save(DataFrameA, file="C:\\Users
\\Desktop
\\DataFrameA.rda")

Subsequently, if you would like to re-load this data, the following code can be utilized:

load(“<filepathway>")

Example:

load("C:
\\Users\\Desktop\\DataFrameA.rda")

However, if you would prefer to have your data saved in a format that can be accessed by programs other than R, you may want to consider saving your data as either a comma separated value file, or as a tab delineated file. The code for accomplishing such is below:

# Saving a as a comma separated value file #

write.table(<Dataframename>, file = "<filepathway>.csv", sep = ",", col.names = NA, row.names = TRUE)

Example:

write.table(DataFrameA, file = "C:
\\Users\\Desktop\\DataFrameA.csv", sep = ",", col.names = NA, row.names = TRUE)

# Saving a as a tab delineated file #

write.table(<Dataframename>, file = "<filepathway>.tsv", sep="\t")

Example:

write.table(DataFrameA, file = "C:
\\Users\\Desktop\\DataFrameA.tsv", sep="\t")

The options: (col.names = NA, row.names = TRUE), prevents a common formatting error from occurring which causes column output to be mislabeled.


Installing Packages and Enabling Packages

If you wanted to download and install packages directly by using the command line interface, you could do so with the following code:

install.packages("<packagename>")

If you would like to use an auxiliary package within your code, you would first have to enable it during your current R session. This can be accomplished by running the code below:

library(<packagename>)


Clear 'R' Workspace

If you would prefer to have a clear workspace during your current R session, you may utilize the following code:

rm(list=ls(all=TRUE))

Clear ‘R’ Console Log

If you would like to clear the log of the ‘R’ console, and you are using a Windows PC, simply press the following keys simultaneously to do so:

Ctrl (+) L

Disable Scientific Notation in ‘R’ Console Log Output

If you would like to disable ‘R’ from outputting data which is scientifically notated, you may utilize the following code:

options(scipen = 999)

Set Zero Values to NA

NA values are not included in R calculations, therefore, it may be useful at times, to change 0 values to NA. This can be achieve with the code below:

<DataFrameName$Variable>[<DataFrameName$Variable> == 0] <- NA

Example:

BaseballPlayers$HR [BaseballPlayers$HR == 0] <- NA

The next article will begin a series of articles pertaining to data modeling within R. Please stay tuned to this blog, as I can promise you that the next batch of entries will be incredibly useful for your data endeavors.

Saturday, August 5, 2017

(R) Functions

The function concept, as it exists within R, is probably the most difficult concept that exists within the R programming language. Moving forward, future entries will address various statistical models, and how to create them within the R platform.

R Functions 

I will be creating this description based on the assumption that you have some understanding as to how a function operates.

In the case below, we are first defining the function "Test_Function". Test_Function will be the name of the function that is called to run the code contained within the braces "{" and "}". In the example function, the value of "X" is being assigned as a value to "Q".

Test_Function <- function(X) is indicating to R, that Test_Function is a function. The "function()" that follows the "<-", provides this definition. The (X) after the function definition, "function(X)", is indicating to R, that "X" will be the value that will be passed into the function.

# The sample data frame below will be utilized throughout the exercises provided: #

A <- c(1,1,1,2,2,3,3)
B <- c(6,5,4,3,2,1)
PlayerID <- c(11,12,13,44,55,16,71)
HR <- c(0,4,6,2,7,10,4)

BaseBallPlayers <- data.frame(PlayerID, HR)

# The Code Below Defines the Function #

Test_Function <- function(X)
{
Q <<- (X)
}

# Run the Function #

Test_Function(B)

# The Variable 'Q' now contains 'DataFrameA' #

Q


"Test_Function(B)" illustrates an example of a function being called. Being "called" is synonymous with being initiated.

In our example, “B” is being passed into the function as it is called. This means that every entry within data vector variable “B”, will now stored within (global) variable “Q”. This is achieved through the usage of "<<-", which informs R that "Q" is to exist as a permanent value.

Here is another function example:

Test_Function2 <- function(Y)

{
Star <<- c(ifelse(Y$HR > 5, "X", " "))
}

BaseBallPlayers$Star <- Test_Function2(BaseBallPlayers)


In this example, we will assume that you are working with a data frame that contains information pertaining to baseball players. The "Star" vector will contain player information pertaining to home run hitting abilities. If a player has hit more than 5 home runs, the vector will mark his place on the list of values with an "X".

It should be mentioned before continuing, that R is strange in the way that it returns values from functions. In the above example, a vector is being created from a column which existed in an already established data frame. (Y) is the variable value that will be replaced by the value of whatever variable is passed into the function. However, without the "<<-", which existed in the above function, R will not return the value as it exists outside of the function. Meaning, that the code within the function will be processed, but the variable which is created as a product of such, will cease to exist after the function has completed its process.

For this reason, you will need to assign the function, and the variable which will be passed to the function, to a new variable prior to calling the function. This allows R to store the variable, which would previously have lived a temporary existence, to a permanent location.

Now for our final example, we'll pretend that you are working with the same player data frame. However, in this scenario, you want to generate the previous vector, and then add it to the existing data frame as a new column. The code for achieving such is below:

Test_Function3 <- function(w){
Star <- ifelse(w$HR > 5, "X", " ")
w$Star <- Star
return(w)
}

BaseBallPlayersA <- Test_Function3(BaseBallPlayers)


In this example, we are returning the value of "w". The reason for such, is that without the utilization of return, the variable that is being assigned the value of “w", would be a vector and not a data frame. Why this is the case is somewhat complicated, but stay with me while I explain it.

Test_Function3 <- function(w){
Star <- ifelse(w$HR > 5, "X", " ")
}


This creates the “Star” variable within the function. Remember, this variable would be temporary unless assigned as a part of the function being called.

w$Star <- Star

Here, the temporary value is being assigned to a new temporary data frame, which will contain it.

If we leave out the return, the temporary value will be the ultimate product of the function, and this value will be assigned to the outside variable. However, with the return specified, R is instructed to return the value of “W,” which has now been modified by the function. However, the modified variable W, still needs a place to stay, as it is temporary, and that is where:

BaseBallPlayersA <- Test_Function3(BaseBallPlayers)

Comes in.

Functions are useful in that they can be utilized to automate every day activities. Or from a more pragmatic standpoint, they can be used to generate daily reports.

In the next article, we will cover a few miscellaneous aspects of R that were overlooked in previous articles, but are nevertheless useful. Subsequently, we will proceed to delve into statistical models, and the R code required to generate reports based on such.

Thursday, August 3, 2017

(R) Using R like SAS

If you are like yours truly, making the transition from SAS to R can leave you longing for certain aspects of the former. It isn’t that R can’t perform many of the similar functions which were native in SAS. However, some of the features that were cornerstones of SAS, are much more obscure in R. This article attempts to highlight some of those key features, and introduce their R equivalents.

# To perform the first series of exercises, please create the sample data frame below: #

ID <- c(1,2,3,4,5,6,7,8,9,10)
Age <- c(19,25,31,30,33,18,22,28,29,30)
H <- c(20,26,18,10,9,12,18,19,20, 11)
HR <- c(5,2,7,5,5,1,1,0,0,10)

BaseballPlayers <- data.frame(ID, Age, H, HR)


Using Conditionals to Subset

In SAS, you can use SQL to subset data sets. Additionally, you also have the ability to create subsetted data sets by utilizing the DATA statement.

For example, if were working with a data set that contained information pertaining to baseball players, and we wanted to create a new set based on players who have hit more than 5 Home Runs, then the code would resemble:

Data HomeRunGreaterFive;
    Set BaseballPlayers;
Where HR > 5;
Run;


In R, the code to perform this function would appear as:

HomeRunGreaterFive <- subset(BaseballPlayers, HR > 5)

In SAS, if you wanted to create a new data set based on players who have more than 15 Hits OR 1 Home Run, the code might be assembled such as:

Data HitsAndHomeRuns;
    Set BaseballPlayers;
Where H > 15 OR HR > 1;
Run;


In R, the code would look like:

HitsAndHomeRuns <- subset(BaseballPlayers, H > 15 | HR > 1)

Finally, if you were programming in SAS, and wanted to create a new data set containing players who have more than 15 Hits and 1 Home Run, the code could be compiled as:

Data HitsAndHomeRuns;
    Set BaseballPlayers;
Where H > 15 AND HR > 1;
Run;


In R, the code would be:

HitsAndHomeRuns <- subset(BaseballPlayers, HR > 5 & H > 4)

Using Conditionals to Delete a Row

This refers to the code statement “delete” in SAS. Such as “IF Age > 30 THEN delete”. Delete, when used in SAS, deletes rows of data that do not match the conditional statement. So, for example, let’s say that you wanted to subset an existing set of baseball players based on player ages. In this scenario, we'll assume that you wanted to create a new set of data that did not include players that were older than 30. This would be achieved with the following SAS code:
Data PlayersYoungerThirty;
    Set BaseballPlayers;
If Age > 30 then delete;
Run;

In R, this code would resemble:

PlayersYoungerThirty <- BaseballPlayers[!(BaseballPlayers$Age > 30),]

Dropping Variables

Again we will return to our baseball example. In SAS, you have the ability to remove variables from a data set by utilizing the drop statement. For this scenario, let’s imagine that you wanted to create a new data set that did not include the eighth, ninth, and tenth variables of an existing data set, as those variable contained extraneous data that was un-needed for your summary set. In SAS, this code would probably look something like:

Data NewCleanSet (Drop = ID age);
    Set BaseballPlayers;
Run;

In R, we can reference those columns by order, and the code would resemble:

# Remove variables: ID; Age #

NewCleanSet <- BaseballPlayers[c(-1, -2)]


Perform a Left Join

R has its own native equivalents for performing data merges. However, the left join, in my opinion, is the cleanest way to accomplish this task. While a "join" is an aspect of SQL, it can be utilized within SAS through the utilization of PROC SQL. If we were going to utilize “left join” to merge two tables within SAS, through SQL functionality, the code would resemble:

proc sql ;
    create table NEWJOINEDTABLE as
    select A.*, B.*
    from TABLEA as A left join TABLEB as B
    on A.DATAONE = B.DATATWO
;
quit;

Now, I’ve done quite a bit of research into how to emulate this functionality within R. The best, hands down approach for accomplishing a similar result, requires the SQLDF package. Once you have that package downloaded, you can perform almost any SQL function within R.

The code above, with the SQLDF package downloaded and enabled, would translate into the R code below:

# Create Example Data Frames: #

DATAONE <- c(1,2,3,4,5)
DATAA <- c("A", "B", "C", "D", "E")

TABLEA <- data.frame(DATAONE, DATAA)

DATATWO <- c(1,2,3,4,5)
DATAB <- c("Spade", "Club", "Diamond", "Heart", "Joker")
TABLEB <- data.frame(DATATWO, DATAB)


# 1. Enable package: 'sqldf' #

library(sqldf)


# 2. Perform Left Join #

NEWJOINEDTABLE <- sqldf('select A.* ,
B.* from TABLEA as A
left join TABLEB as B
on A.DATAONE = B.DATATWO')

Utilizing PROC FREQ

I searched far and wide for a decent replacement for the absolutely superb PROC FREQ statement of SAS. The closest that I could come to the original SAS iteration, requires a package. Therefore, for this method to work, you will need to download the ‘gmodels' package. Also, make sure you have it enabled when running the R code equivalent. 

In SAS, the code to generate a frequency table containing home run information from DataTableA is:

Proc Freq Data = DataTableA;
    tables HR;
Run;

In R would look like (with 'gmodels' downloaded/enabled):

library(gmodels)

CrossTable(DataTableA$HR)

Adding Leading Zeroes

Always a problem, regardless of system, losing leading zeroes, and subsequently having to re-add them, is truly a burden on any data professional. There is an entire entry on this blog, on how to accomplish this in SAS. Here is how to accomplish re-adding leading zeroes in R. Please be aware, that if your column data contains numerical information, that utilizing this method changes the data to character type.

# Example Data Frame #

modifiedVar <- c(1,2,3,4,5,6,7,8)

DATAFRAME <- data.frame(modifiedVar)


# Code #

DATAFRAME$modifiedVar <- sprintf("%04d", DATAFRAME$modifiedVar)


%04d specifies the total length of the variable.

So if the above code was being utilized to on the following column variables, the following results would occur:

Old Var = 1
New Modified Var = 0001

Old Var = 10
New Modified Var = 0010

Old Var = 100
New Modified Var = 0100

If you wanted to add additional zeroes, you would simply need to change the “%04d” option to a larger value.

Dropping Tables

This code is a native SQL function. However, I still use it within SAS by utilizing the PROC SQL statement.

If you wanted to drop three tables within SAS though the usage of the drop statement, the code would resemble:

Proc SQL;
    drop tableA, tableB, tableC;
quit;

To achieve the same result while using R:

rm(DATAFRAME, BaseballPlayers)

Fixing Dates

Finally, we come to dates, which to every SAS user, is the bane of their existence. I will not go through how to modify SAS dates in this article, as there is an independent post dedicated to such on this blog. However, below are four different lines of code.

# Example Data Frames #

DateA <- c("01/20/2020", "02/13/1980", "03/30/1970", "04/13/1991")

DataFrameA <- data.frame(DateA)

###################################################

DateB <- c("01/25/2020", "02/21/1980", "05/30/1970", "09/13/1998")

DataFrameB <- data.frame(DateB) 

These two lines are for changing the data type of variable columns, within an existing data frame, to a date type format.

DataFrameA$DateA <- as.Date(DataFrameA$DateA, format="%m/%d/%Y")

DataFrameB$DateB <- as.Date(DataFrameB$DateB, format="%m/%d/%Y")


Once this is accomplished, you have the ability to create a new column, which will contain the number of days elapsed between the two dates:

DataFrameA$DaysDifference <- difftime(DataFrameA$DateA, DataFrameB$DateB, units = 'days')

You could also perform a similar function, and generate a new column which contains the number of weeks elapsed:

DataFrameA$WeeksDifference <- difftime(DataFrameA$DateA, DataFrameB$DateB, units = "weeks")

The next article should be posted within a few days, I have yet to decide on a specific topic to discuss. In the interim, please continue to visit my blog, I appreciate your patronage.

Monday, July 31, 2017

(R) Conditionals

Today we will be discussing conditional statements within R. Conditionals are very easy to understand, and extremely powerful when implemented. In typical fashion, I will first address a coding concept, followed by an example of the code being utilized.

If you are familiar with generally practiced coding standards and paradigms, you should be familiar with conditional statements.

Typically, in other languages, an IF statement would resemble something like:

if (condition is met) DOSOMETHING;

The exact structuring of the statement depends on the coding language.

In R, conditional coding resembles the following:

ifelse(condition, if true do this, if false do this)

Example:

For this example, we will pretend that you are again using the iconic DataFrameA, and in this particular scenario, you want to create a flag variable within a blank data column.

# First we will create our sample data frame with the code below: #

A <- c(1,1,1,2,2,3,3)
B <-c(2,1,3,2,3,3,1)
DataFrameA <- data.frame(A, B)
DataFrameA

#########################################################

DataFrameA

A  B  C
1   2
1   1
1   3
2   2
2   3
3   3
3   1

The code that you will create, will check both column A, and column B, if either column contains a row value that matches, an 'X' will be created in column C.

To achieve this, the following line of code can be utilized:

DataFrameA$C <- ifelse(DataFrameA$A == DataFrameA$B, 'X', ' ')

Additionally, if you wanted to create code that creates an 'X' value for a match, or a 'Y' value for a non-matching variable, the following code can be utilized:

DataFrameA$C <- ifelse(DataFrameA$A == DataFrameA$B, 'X', 'Y')

In the first example, the newly modified DataFrameA would resemble:

A  B  C
1   2
1   1  X
1   3
2   2  X
2   3
3   3  X
3   1

In the second example, the newly modified DataFrameA would resemble:

A  B  C
1   2  Y
1   1  X
1   3  Y
2   2  X
2   3  Y
3   3  X
3   1  Y

A few quick notes on conditionals in R. Please note the use of '==' instead of '=' in the above listed example. In R, '==' is used to assess conditions, not '='. Also, DataFrameA$C is referring to the column C in DataFrameA, DataFrameA$A is referring to column A in DataFrameA, and DataFrameA$B is referring to column B in DataFrameA.

These examples are simple, but the applications for this concept are endless. In the next article, we will be discussing some of the similarities between R and SAS, and how to achieve similar functionality in R as it pertains to SAS.

Thursday, July 27, 2017

(R) Data Frame Maintenance

The topic of today's post is: Data Frame Maintenance. In this article, I will demonstrate various techniques that can be utilized to accomplish the tasks associated with such.

Let's say, for example, that you are working with a data frame named: "DataFrameA". For whatever reason, the third column of this particular data frame needs to be re-named. The code to accomplish this task is below:

colnames(DataFrameA)[<#ofcolumntochange>] <- "New Column Name"

So, if you wanted to change the name of the third column of DataFrameA to, “DataBlog", the code would resemble:

colnames(DataFrameA)[3] <- "DataBlog"


Changing Column Variable Type

Now, let's say that you wanted to change the data type that is contained within a column of an existing data frame. Again, we will use "DataFrameA" for our example.

This code will change a column which contains integers, to a column that contains factors:

DataFrameA$VarA <- as.factor(DataFrameA$VarA)

This code will change a column which contains factors, to a column that contains integers:

DataFrameA$VarA <- as.integer(DataFrameA$VarA)

This code will change a column which contains factors, to a column that contains characters:

DataFrameA$VarA <- as.character(DataFrameA$VarA)


Stacking Data Frames

Perhaps you want to stack two data frames, one on top of the other.

If each data frame has the same column names, then the following code is ideal:

NewDataFrame <- rbind(topdataframe, bottomdataframe)

If one data frame contains an additional column that is not included within the other, you will need to add the missing column to the data frame before stacking the data.

For example, if Data Frame A contains:

A    B     C
1     9      4
2     18    8
3     27   12
4     36   16

And Data Frame B contains:

A    B
1     3
2     6
3     9
4     12

You would first need to add a column containing missing values to the bottom data frame by running the example code:

DataFrameB$C <- NA


This code modifies Data Frame B so that it resembles:

A     B     C
1      3      NA
2      6      NA
3      9      NA
4      12    NA

The data frames can now be stacked with the code:

NewDataFrame <- rbind(DataFrameA, DataFrameB)

And the new data frame will resemble:

A    B      C
1     9       4
2     18     8
3      27    12
4      36    16
1      3      NA
2      6      NA
3      9      NA
4      12    NA


Adding a Vector as a Column

For this example, we'll pretend that you wanted to add a new column, in the form of a vector, to an existing data frame.

If the column is of the same length, row wise, then adding it to a data frame is simple.

Utilize the code:

DataFrameName$NewColumnName <- NewColumntoAdd

If the column is shorter, row wise, in comparison to the data frame in which it is being added, then you will first have to add additional values to the vector before utilizing the above code.

For example, if NewColumntoAdd is 35 rows in length, and DataFameA is 36 rows in length, you could add the additional values needed to complete the subsequent task with the following code:

AdditonalDataVector <- rep(c(NA), times=1) # Or however many NA rows are needed #

NewColumntoAdd <- c(NewColumntoAdd, AdditionalDataVector)


Now you can successfully run the code:

DataFrameName$NewColumnName <- NewColumntoAdd


Re-Ordering Columns within a Data Frame

To accomplish this task you have two options.

The first option is to re-order the column data by column name.

So for example, if you were working on a data frame (DataFrameA), with the column names of ("A", "B", "C", "D"), and you wanted to re-order the columns so that they were displayed such as ("B", "C", "A", "D"), you could run the code:

DataFrameA <- DataFrameA[c("B", "C", "A", "D")]


You also have the option of re-ordering the columns by column number.

If this was the option that you wished to utilize, the code would resemble:

DataFrameA <- DataFrameA[c(2,3,1,4)]

That is all for this entry. I have not yet decided what the topic for the next post, but I promise you that it will contain more helpful R related information.

Sunday, July 16, 2017

(R) Data Frame Extraction

In this article, we will be discussing how to extract data from existing data frames within R.

If you aren’t already familiar with the function of braces( ‘[‘ and ‘]’ ) within R, we will briefly review their usage.

When you encounter braces in R, the variables specified within the braces themselves, are instructing R to query and return data.

[ X , Y ]

Above is an example of how such a query would appear within the R code base.

X - Specifies Row

Y - Specifies Column


So if a programmer were to write the code:

E <- DataFrameA[ 1 , 2, drop = FALSE]

R would interpret this to mean: return the data from Row: 1, Column: 2, and store this data in factor variable ‘E’.

Leaving either the left or the right position empty in a braces related query, instructs R to return ALL data.

Therefore:

E <- DataFrameA[ 1 ,  , drop = FALSE]

Would instruct R to return ALL Column data from Row:1. (And store this data in ‘E’)

While:

E <- DataFrameA[ , 1 ] 
Would instruct R to return ALL Row data from Column:1. (And store this data in ‘E’)

The following are examples of code samples which extract data from R Data Frames.

E <- DataFrameA[3, 2, drop = FALSE] Extracts the third element in the second column of DataFrameA, and stores that element in factor variable ‘E'.

E <- DataFrameA[c(1 , 3), 2, drop = FALSE] Extracts the data within row 1 and row 3, within column 2, of DataFrameA. The data is then stored in factor variable ‘E’.

E <- DataFrameA[5, ] Extracts row 5, and all column data contained within row 5. The data will be stored in DataFrame ‘E'.

E <- DataFrameA[ , 8] Extract all rows data from column 8. The data is then stored in factor variable ‘E’.

Saturday, July 15, 2017

(R) Vector Creation and Extraction

In this article, we will discuss how to create vectors manually, and also, how to create vectors from data contained within existing vectors.

Just as a reminder, a vector is a sequence of data elements of the same basic type. Each element in a sequence is referred to as a component.*

Elements of a vector are displayed to the console such as:

[1] 3 5 7 9


However, just because the data is printed as a row, does not mean that the data cannot be added to an existing data frame as a column. Therefore, though vector data prints to the console horizontally, you can imagine it as also existing vertically, like so:

3
5
7
9


The [1] indicates the beginning of the vector. If the console runs out of horizontal space while printing the vector, the remainder of the vector will be printed to the next line of the console, the new line will begin with a value which indicates the sequence order.

For Example:

[1] 1 2 5 7 9
[6] 11 13 15 17


Vector Creation

Here are a few examples vector creating code:

x <- seq(from=2, to=12, by=2)

This creates a vector which contains the values: 2 4 6 8 10 12

The code is instructing R to count from 2 to 12, by 2, and then store the values in vector 'x'.

x <- rep(seq(from=2, to=12, by=2), times=2)

This creates a vector which contains the values: 2 4 6 8 10 12 2 4 6 8 10 12

The code is instructing R to count from 2 to 12, by 2, twice, and then store the values in vector 'x'.

x <- rep(c("o", "m", "g"), times=3)

This creates a vector which contains the values: O M G O M G O M G

This code is instructing R to repeat the values "O", "M" "G", three times, and then store the values in vector 'x'.

Now, let's say that you want to manipulate the data within the vectors, here are few methods.


Vector Data Manipulation

Adding Within Vectors

x <- x + 10 


This code adds the value of 10 to each value within vector x, and then stores the values within vector 'x'.

So if vector 'x' contained the data: 1 2 3 4 5 6

The above code would modify the vector so that it contained the data: 11 12 13 14 15 16


Multiplying Within Vectors

x <- x * 0

This code multiplies each value within vector 'x' by the value of 0. The values are then subsequently stored within vector 'x'.

So if vector 'x' contained the data: 1 2 3 4 5 6

The above code would modify the vector so that it would contain the data: 0 0 0 0 0 0

If two vectors are of the same length, they may be added, subtracted, multiplied or divided.

For Example:

If vector 'y' contained the values: 2 2 2 2 2

And vector 'x' contained the values: 2 2 2 2 2

The Code:

w <- x + y

Would generate 'w' as a vector, containing the values of: 4 4 4 4 4

If vectors of different lengths are combined in this way...

For Example:


If vector 'y' contained the values: 2 2 2 2 2

And vector 'x' contained the values: 2 2 2 2 2

w <- x + y

Would present the user with the error:

Warning message: In x + y : longer object length is not a multiple of shorter object length.

Extracting From Vectors

Assuming that 'u' is a vector which contains the values of: 1 2 3 4 5 6

q <- u[3]

This example would extract the third value of the vector 'u', and store the data in the vector 'q'.

Therefore, 'q' would contain a value of 3.

q <- u[-3]

In this case, all values but the third value of vector 'u' would be extracted, and the data would be stored in vector 'q'.

Therefore, 'q' would contain the values: 1 2 4 5 6

q <- u[1:2]

Here, the first two values are extracted from u, and stored in vector 'q'.

Therefore, 'q' would contain the values: 1 2

q <- u[c(1,2)]

This is another way of extracting the first two values of 'u', which will then be stored in vector 'q'.

Again, 'q' would contain the values: 1 2

q <- u[-c(1,2)]

In this example, all values from vector 'u' are extracted, except the values of 1 and 2.

Therefore, vector 'q' would contain the values: 3 4 5 6

q <- u[u<4]

This method extracts all values within vector 'u' that are less than 4.

It is for this reason, the vector 'q' would contain the values: 1 2 3

* - www.r-tutorial.com/r-introduction/vector

Saturday, July 8, 2017

(R) Checking Data Integrity

I wanted to address, before moving to the topic of data integrity, two additional methods that can be utilized for importing data into the R platform. I do not personally utilize either of these methods due to their reliance on the user interface. The prior methods discussed, leave import records within the code. These recorded pathways will be helpful to the user who must return to a project at a later date.

However, if you were specifically searching for a more user friendly method to utilize when importing data, the following methods may better suit your needs.

The easiest method to utilize when importing data, is the following. This particular data import method leaves absolutely no record for the user, and R-Studio must be installed for this method to successfully execute.

First, you will need to open R-Studio. After this has been accomplished, you will need to click on the drop down menu option that reads: "Import Dataset".


Each data set variation requires that you install a certain R-Package before proceeding. If you have previously installed the required package that is necessary for the file variant, you will be able to proceed with the import.

The other method that can be utilized to import data into R, is a hybrid of code and user interaction.

You will need to run the following code template from the R console.

<datasetname> <- read.table(file.choose(), <importoptions>)

So, if we were to utilize this template on our previous examples, the code would resemble the following:

(Assuming that the file is a .csv)

DataFrameA <- read.table(file.choose(), fill = TRUE, header=TRUE, sep="," )

(Assuming that the file is tab delineated)

DataFrameA <- read.table(file.choose(), fill = TRUE, header=TRUE, sep="\t" )

Either variation will cause your operating system to open a window which contains the file interface of native to your system.


From this user interface, you will be able to select the file that you would like to import into R.

Checking Data Integrity

After your data has been successfully imported into R, you should check the integrity of the data to make sure that all of the original data was imported correctly. Listed below, are some of the commands that can be used to ensure that data integrity was maintained.

<DataFrameName>[c(1, 2, 3), ] - Displays the row data contained within the first three rows of the data frame, and all corresponding column data.

<DataFrameName>[1:3, ] - Performs the same action as the above command. However, if additional rows are required for viewing, this command does not necessitate the selection of each particular row in the command option.

dim(<DataFrameName>) - This command displays the dimensions of the selected data frame. Information is displayed as Row x Column.

summary(<VarName>) - This command produces an abridged statistical summary of all numerical data, and a frequency distribution of all non-numerical data.

levels(<VarName>) - This command displays all variable variations in the selected variable column.

class(VarName) - This command will indicate the variable type of the specified variable listed.

You have the option of printing the entire data set to the console. However, this is only feasible if the data contained within the data frame is not overly large. If you do choose to print the data frame data to the console, I would recommend enabling the option below before proceeding. This option enlarges console output width, which allows for the printed data to display correctly.

options("width"=200)

The command below prints the data frame to the console:

print(<DataFrameName>)

If the utilization of this command is infeasible due to the size of the data frame, you could instead utilize the head or tail commands.

The head command template is:

head(<DataFrameName>, n=<number of rows to display>)

Executing this command will display the first n number of rows contained within the data frame.

Example:

# Print the first 10 rows of the data set #

head(DataFrameA, n=10)

The tail command template is:

tail(<DataFrameName>, n=<number of rows to display>)

Executing this demand will display the last n number of rows contained within the data frame.

Example:

# Print the last 5 rows of the data set #

tail(DataFrameA, n=5)

The "fix" command performs a similar command to the previous commands listed. In many cases, it may be best to run this command initially when checking for data integrity before proceeding with other commands.

"fix" allows the user to edit, through a graphical user interface which is launched subsequently to the command's execution, individual data entries within the data frame. Additionally, the user will also be presented with the opportunity to change variable type data. The downside to this particular command, is that it only presents the first 5000 rows of data. Also, there will be no record left within the code which indicates whether any data modifications took place.

# Allows the user to edit the first 5000 observations #

fix(DataFrameName)

There are two other commands which you should also familiarize yourself with, though they are very similar in function to the commands which were previously discussed. Those commands being: "sapply", and "str".

"sapply", if utilized in a manner similar to the example below, will display all variables within a data frame, and their corresponding data type, to the console window.

sapply(DataFrameName, class)

"str" if utilized in the manner displayed below, will display dimensional data pertaining to the data frame, level data pertaining to character type variables, each variable's type, and the first few variable entries for each corresponding variable.

In the next article, I will discuss how to generate data summaries and measure for frequency within R. Additionally, I will also address how to export R data, and how to save data frames in .rda format.

Monday, July 3, 2017

(R) Establishing Working Directory & Importing Data

This is the first article, of what will probably be many articles, pertaining to R-Software. I am assuming that you are familiar with R-Software, and that you have the software installed. Additionally, I am also assuming that you have RStudio, the IDE, also installed.

Once you have the R Console open, you will first want to set your working directory.

This can be achieved with the command:

setwd("<pathway of working directory>")

For example, you could create a designated folder on your Window's Desktop for such a directory, and make that folder your working directory. The code for such would resemble:

setwd("C:/Users/Name/Desktop/RWorkDirectory")

It is important to note that you will have to change the default "\" to "/", as R does not utilize the backslash in path directory listings.

The advantage for establishing a working directory, is that it allows for a certain level of convenience in importing, exporting, and saving data.

For example, if you were importing data without establishing a working directory, the code template for such would resemble:
(Assuming that the file is a .csv)

DataFrameA <- read.table("C:/Users/Name/Desktop/RWorkDirectory/Filename.csv", fill = TRUE, header = TRUE, sep = "," )

or

(Assuming that the file is tab delineated)

DataFrameB <- read.table("C:/Users/Name/Desktop/RWorkDirectory/Filename.txt", fill = TRUE, header = TRUE, sep = "\t" )

If you had established the working directory, the code statement would be much shorter:

DataFrameA <- read.table("Filename.csv", fill = TRUE, header=TRUE, sep="," )

or

DataFrameA <- read.table("Filename.txt", fill = TRUE, header=TRUE,  sep="\t" )

Import Options

Fill, Header, and Sep are optional statements, but typically their inclusion is necessary. Here is what each option enables:

Fill - This option notifies R that the variable observation data is of unequal length, and that some records will be missing observational data. In the case of missing data, 'N/A' values will be added if this option is enabled. 

Header - This indicates to R, that the first row of data contains column names.

Sep - This indicates the type of delineation that separates each data observation. "," indicates a comma separated file, and "\t" indicates a tab delineated file. Additionally, if the data values are separated by some other exotic format, (ex. #, @, or |), you can indicate this as an import option, by listing it after sep =. Ex sep = "|".

Get Working Directory

If you ever forget where your work directory is located, you can always have it printed to the console by utilizing the command:

getwd()

In our example case, running the above command should output:

C:/Users/Name/Desktop/RWorkDirectory

In the next article, I will discuss how to check the integrity of newly imported data.