Easiest way to create data frame in R – R tutorial


R has the ability to import and export data in many formats. In case if you are looking to enter data in R just like the way you enter a data in a spreadsheet, then here is the trick for you. Creating a data frame in R is easy. Not let us see how to enter data just like we enter data in a spreadsheet.

r data entry

The simplest method of data entry is from the keyboard. The edit() function in R will popup an editor that will allow you to enter your data manually. Just like the one shown in the above picture. Follow these two simple steps:
1. Create an empty data frame (or matrix) with the variable names and modes you want to have in the final dataset.
2 Invoke the editor on this data object, enter your data, and save the results back to the data object.


In the following example, let us create a data frame named data with three variables: age (numeric), gender (character)and weight (numeric). Then invoke the editor, add your data, and save the results. Job is done!

data <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0))
data <- edit(mydata)

Assignments like age=numeric(0) create a variable of specific data type, but without actual data. Note that the result of the editing is assigned back to the object itself. The edit() function operates on a copy of the object. If you don’t assign it a destination, all of your edits will be lost!. so Don’t forget to use the 2nd code in the above example.

You can even create a new variable in the editor and specify the data type, whether it is a numeric or character type.


The shortcut command you can use instead of edit() function is making use of fix() function. In the above example, you may also use fix(data) this is pop up the data editor.

This method of data entry works well for small datasets. For larger datasets, it is probably good to use the methods of importing data from existing text files, Excel spreadsheets, statistical packages, or database management systems.


Please enter your comment!
Please enter your name here