Creating a Data Frame in R Studio

Creating a Data Frame in R Studio


Today, I will show you how to create a data frame using R Studio. We have several variables that we will combine into a data frame. The ‘nation’ variable consists of five countries: “USA”, “GERMANY”, “NETHERLANDS”, “DENMARK”, and “KOREA”. We also have some survey data on the happiness and economic power of each country.

nation=c("USA", "GERMANY", "NETHERLANDS", "DENMARK", "KOREA")
happiness=c(85,89,90,91,77)
economy=c(95,94,87,84,92)

To create a data frame in R, we can use the data.frame() function to combine all variables. In this example, I have written the code as shown above. The resulting data frame is named dataA.

dataA=data.frame(nation,happiness,economy)
dataA

   nation      happiness     economy
1  USA         85            95
2  GERMANY     89            94
3  NETHERLANDS 90            87
4  DENMARK     91            84
5  KOREA       77            92

You can now view the resulting data frame by calling dataA in your R console.



Using matrix() function

You can also write data as a matrix and convert it to a data frame. First, use the matrix() function to enter all the data and declare that you want to create four columns (ncol=3).

dataA= matrix(c("USA","GERMANY","NETHERLANDS","DENMARK","KOREA",85,89,90,91,77,95,94,87,84,92), ncol=3)
dataA

       [,1]          [,2]   [,3]
[1,]   "USA"         "85"   "95"
[2,]   "GERMANY"     "89"   "94"
[3,]   "NETHERLANDS" "90"   "87"
[4,]   "DENMARK"     "91"   "84"
[5,]   "KOREA"       "77"   "92"

Then, assign the variable dataA to the matrix and convert it to a data frame using the as.data.frame() function. Let’s then execute the newly assigned variable dataB.

dataB= as.data.frame(dataA)
dataB

    V1           V2   V3
1   USA          85   95
2   GERMANY      89   94
3   NETHERLANDS  90   87
4   DENMARK      91   84
5   KOREA        77   92

The data has been transferred, but the column names are missing. In this case, we need to enter new column names using the names() function.

names(dataB)= c("Country","happiness","economy")
dataB

   Country      happiness   economy
1  USA          85          95
2  GERMANY      89          94
3  NETHERLANDS  90          87
4  DENMARK      91          84
5  KOREA        77          92


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.