Warning: Undefined array key "options" in /htdocs/wp-content/plugins/elementor-pro/modules/theme-builder/widgets/site-logo.php on line 192
Create a data frame with RStudio - Rstudio-data
Start with R

Create a data frame with RStudio

Share

If you want to tackle R, it is essential to start by creating a dataframe.. Creating a data frame with RStudio is a relatively simple process. You need two things: data and the RStudio software. Creating a data frame is done using the data.frame() function. You can either enter your data manually into RStudio as a table, or import it from an external file, such as a CSV file.

Here we will opt for manual input and start by creating a data frame with 3 lists of data.

  • A list of people
  • A list of sizes
  • A list of weights

The sizes & weights will be the values of our data frame, while the people will be our “lines”. For this we have several solutions:

Create a data frame with the data.frame() function

The first method consists in adding the values one after the other directly in the data.frame() function. Thus, we will start by adding our two lists of values, namely the size and the weight, before defining the list of rows via the row.names() variable.

df <- data.frame(size = c(177,167,181,179,168,175),
+   weights = c(71,68,78,75,68,64),
+   row.names = c("pers1","pers2","pers3","pers4","pers5","pers6") 
+ )

df
             size weight
pers1    177    71
pers2    167    68
pers3    181    78
pers4    179    75
pers5    168    68
pers6    175    64

Use c() lists to create a data frame

The second method is to define the elements and associated values independently by compiling them into lists, before adding them to the data.frame() function. Thus we make a size list, a weight list and a name/person list.

size <- c(177,167,181,179,168,175)
weight <- c(71,68,78,75,68,64)
names <- c("pers1","pers2","pers3","pers4","pers5","pers6")

df2 <- data.frame(size,weight, row.names = names)

df2
##              size weight
## pers1    177    71
## pers2    167    68
## pers3    181    78
## pers4    179    75
## pers5    168    68
## pers6    175    64

Thus, we can see that our 2 dataframes have exactly the same information. We can compare the two structures with the function of the same name str().

> str(df)
'data.frame':	6 obs. of  2 variables:
 $ size : num  177 167 181 179 168 175
 $ weight : num  71 68 78 75 68 64

> str(df2)
'data.frame':	6 obs. of  2 variables:
 $ size : num  177 167 181 179 168 175
 $ weight : num  71 68 78 75 68 64

That’s it, you have created your first data frame.

Reminder of functions & variables

dataframe(), function allowing the creation of a data frame

row.names =, variable allowing to define the lines in a data frame

c(), allows you to add values in the form of a list

str() function displaying the structure of a data frame (number of observations, number of variables, class, …)

Tags:

Next Up