What is a data frame in R.

Todo list online

A data frame can be expanded with new variables in R. For example, you can get data from another player on Grandma’s team. Or you may want to calculate a new variable from the other variables in the dataset, such as the total of the baskets created in each game.

Add a single variable

There are three ways to add a variable. Similar to adding observations, you can use either the cbind () function or the indexes.

You can also use the dollar sign to add an additional variable. Imagine grandma asked you to include the number of her friend Gabrielle's baskets in the data frame. First you would create a vector with this data like this: >> Baskets. of. Gabrielle <- c="" (11,="" 5,="" 6,="" 7,="" 3,="" 12,="" 4,="" 5,="">

To create an additional variable named Gabrielle with this data, just do the following: >> Baskets. df $ Gabrielle <- körbe.="" von.="" gabrielle="">

If you want to check whether this worked, but do not want to display the entire data frame, you can use the head () function. This function has two arguments: the object to be displayed and the number of lines you want to see. To see the first four lines of the new data frame, baskets. Use the following code:

>> head (baskets. Df, 4) Grandma Geraldine Gabrielle 1. 12 5 11 2. 4 4 5 3. 5 2 6 4. 6 4 7

Multiple variables with cbind

Add Imagine your data frame is a matrix and use the cbind () function to do so. Unlike using rbind () in data frames, you don't even have to worry about the row or column names. Let's create a new data frame with the goals for Gertrude and Guinevere. To combine both into one data frame, try: >> new. df <- daten.="" rahmen="" (+="" gertrude="c" (3,="" 5,="" 2,="" 1,="" na,="" 3,="" 1,="" 1,="" 4),="" +="" guinevere="c" (6,="" 9,="" 7,="" 3,="" 3,="" 6,="" 2,="" 10,="" 6)="" +="" )="">

The line names of the data frames are new. df and baskets. df differ, R ignores this and just uses the line names of the first data frame in the cbind () function, as you can see from the output of the following code: >> head (cbind (baskets. df, new .df), 4) Grandma Geraldine Gabrielle Gertrude Guinevere 1.125.11.36 2.4 4 5 5 9 3. 5 2 6 2 7 4. 6 4 7 1 3

When using a data frame or matrix with column names, R uses these as the name of the variable. When you use cbind () to add a vector to a data frame, R uses the vector's name as the variable name unless you specify it yourself, as you did with rbind ().

If you bind a matrix to the data frame with no column names, R will automatically use the column numbers as names, but this will cause some problems because simple numbers are invalid object names and are therefore more difficult to use as variable names. In this case, you'd better use the indexes. If you want to use a data frame and don't have to keep typing its name followed by $, you can use the functions with () and inside (). You can also easily add variables to a data frame using the inside () function.