Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Excel’s greatest strength — and its greatest weakness — is the ease of users to manipulate the basic data. Done well and the capability affords flexibility; done poorly and you are left with hard-coded cells and hidden Column A’s.
With R you approach the data differently by writing each step in code. Generally you do not manipulate the basic data itself and instead write a series of steps in code.
Related: 5 Things Excel Users Should Know About R
While this is a general rule of thumb of course it’s a generalization. Excel for its part has the VBA programming language and other advanced techniques for solid workbook design. And R does indeed have a spreadsheet-like data entry tool.
Here’s how to work it:
1. Create an object
Everything in R is an object and this is a fundamental distinction between R and Excel.
While we can launch a spreadsheet-like viewer for data entry, we first need to pass our data to an object.
To do this we will set up a blank data frame (similar to an Excel table with rows and columns). Leaving the arguments blank in data.frame will result in an empty data frame
myData <- data.frame()
Next we will use the edit function to launch the viewer. Note that I pass our myData data frame back to the myData object — this way the changes we make to this module will be “saved” to the original object.
myData <- edit(myData)
Running this command the Data Editor launches. I am going to complete the table with this small roster.
Name | Height | IsInjured |
---|---|---|
Smith | 60 | FALSE |
Wright | 57 | FALSE |
Armstrong | 61 | TRUE |
Change variable names by clicking on their labels and typing your changes. You can also set variables as numeric or character. Note though that you cannot set a variable to logical (the third column); we will have to do that in the syntax editor.
Once your data looks like the above, close the table. Changes are saved automatically.
3. Close & load
Check out the resulting data frame by printing it.
myData is.logical(myData$IsInjured) myData$IsInjured <- as.logical(myData$IsInjured)
As mentioned earlier, the data editor does not set columns to logical. We will confirm this by using the is.logical function.
Set the column to logical by using as.logical. The column’s mode changes from character to logical.
See the complete code here…
#create blank data frame myData <- data.frame() #edit data in the viewer myData <- edit(myData) #close & load myData #change IsInjured to Logical is.logical(myData$IsInjured) myData$IsInjured <- as.logical(myData$IsInjured)
When will I use this?
As a whole, Excel offers much greater capabilities for data entry. But as a new R user I see this as a powerful tool for starting out, especially if you get stuck renaming columns.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.