QUESTION: How do I make a basic data table from a vector?

If you have a vector in R, how do you make a data table from it?

Data

We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library("“palmerpenguins”), and load the data with data(penguins)

#install.packages("palmerpenguins")
library(palmerpenguins)
library(pander)
data(penguins)

We’ll subset out the column Y and Z into seperate vectors to set things up

X <- penguins$body_mass_g
Y <-penguins$bill_length_mm

Next we’ll create a dataframe

df <- data.frame(X,Y)

Next we’ll plot this using pander

pander(df)
X Y
3750 39.1
3800 39.5
3250 40.3
NA NA
3450 36.7
3650 39.3
3625 38.9
4675 39.2
3475 34.1
4250 42
3300 37.8
3700 37.8
3200 41.1
3800 38.6
4400 34.6
3700 36.6
3450 38.7
4500 42.5
3325 34.4
4200 46
3400 37.8
3600 37.7
3800 35.9
3950 38.2
3800 38.8
3800 35.3
3550 40.6
3200 40.5
3150 37.9
3950 40.5
3250 39.5
3900 37.2
3300 39.5
3900 40.9
3325 36.4
4150 39.2
3950 38.8
3550 42.2
3300 37.6
4650 39.8
3150 36.5
3900 40.8
3100 36
4400 44.1
3000 37
4600 39.6
3425 41.1
2975 37.5
3450 36
4150 42.3
3500 39.6
4300 40.1
3450 35
4050 42
2900 34.5
3700 41.4
3550 39
3800 40.6
2850 36.5
3750 37.6
3150 35.7
4400 41.3
3600 37.6
4050 41.1
2850 36.4
3950 41.6
3350 35.5
4100 41.1
3050 35.9
4450 41.8
3600 33.5
3900 39.7
3550 39.6
4150 45.8
3700 35.5
4250 42.8
3700 40.9
3900 37.2
3550 36.2
4000 42.1
3200 34.6
4700 42.9
3800 36.7
4200 35.1
3350 37.3
3550 41.3
3800 36.3
3500 36.9
3950 38.3
3600 38.9
3550 35.7
4300 41.1
3400 34
4450 39.6
3300 36.2
4300 40.8
3700 38.1
4350 40.3
2900 33.1
4100 43.2
3725 35
4725 41
3075 37.7
4250 37.8
2925 37.9
3550 39.7
3750 38.6
3900 38.2
3175 38.1
4775 43.2
3825 38.1
4600 45.6
3200 39.7
4275 42.2
3900 39.6
4075 42.7
2900 38.6
3775 37.3
3350 35.7
3325 41.1
3150 36.2
3500 37.7
3450 40.2
3875 41.4
3050 35.2
4000 40.6
3275 38.8
4300 41.5
3050 39
4000 44.1
3325 38.5
3500 43.1
3500 36.8
4475 37.5
3425 38.1
3900 41.1
3175 35.6
3975 40.2
3400 37
4250 39.7
3400 40.2
3475 40.6
3050 32.1
3725 40.7
3000 37.3
3650 39
4250 39.2
3475 36.6
3450 36
3750 37.8
3700 36
4000 41.5
4500 46.1
5700 50
4450 48.7
5700 50
5400 47.6
4550 46.5
4800 45.4
5200 46.7
4400 43.3
5150 46.8
4650 40.9
5550 49
4650 45.5
5850 48.4
4200 45.8
5850 49.3
4150 42
6300 49.2
4800 46.2
5350 48.7
5700 50.2
5000 45.1
4400 46.5
5050 46.3
5000 42.9
5100 46.1
4100 44.5
5650 47.8
4600 48.2
5550 50
5250 47.3
4700 42.8
5050 45.1
6050 59.6
5150 49.1
5400 48.4
4950 42.6
5250 44.4
4350 44
5350 48.7
3950 42.7
5700 49.6
4300 45.3
4750 49.6
5550 50.5
4900 43.6
4200 45.5
5400 50.5
5100 44.9
5300 45.2
4850 46.6
5300 48.5
4400 45.1
5000 50.1
4900 46.5
5050 45
4300 43.8
5000 45.5
4450 43.2
5550 50.4
4200 45.3
5300 46.2
4400 45.7
5650 54.3
4700 45.8
5700 49.8
4650 46.2
5800 49.5
4700 43.5
5550 50.7
4750 47.7
5000 46.4
5100 48.2
5200 46.5
4700 46.4
5800 48.6
4600 47.5
6000 51.1
4750 45.2
5950 45.2
4625 49.1
5450 52.5
4725 47.4
5350 50
4750 44.9
5600 50.8
4600 43.4
5300 51.3
4875 47.5
5550 52.1
4950 47.5
5400 52.2
4750 45.5
5650 49.5
4850 44.5
5200 50.8
4925 49.4
4875 46.9
4625 48.4
5250 51.1
4850 48.5
5600 55.9
4975 47.2
5500 49.1
4725 47.3
5500 46.8
4700 41.7
5500 53.4
4575 43.3
5500 48.1
5000 50.5
5950 49.8
4650 43.5
5500 51.5
4375 46.2
5850 55.1
4875 44.5
6000 48.8
4925 47.2
NA NA
4850 46.8
5750 50.4
5200 45.2
5400 49.9
3500 46.5
3900 50
3650 51.3
3525 45.4
3725 52.7
3950 45.2
3250 46.1
3750 51.3
4150 46
3700 51.3
3800 46.6
3775 51.7
3700 47
4050 52
3575 45.9
4050 50.5
3300 50.3
3700 58
3450 46.4
4400 49.2
3600 42.4
3400 48.5
2900 43.2
3800 50.6
3300 46.7
4150 52
3400 50.5
3800 49.5
3700 46.4
4550 52.8
3200 40.9
4300 54.2
3350 42.5
4100 51
3600 49.7
3900 47.5
3850 47.6
4800 52
2700 46.9
4500 53.5
3950 49
3650 46.2
3550 50.9
3500 45.5
3675 50.9
4450 50.8
3400 50.1
4300 49
3250 51.5
3675 49.8
3325 48.1
3950 51.4
3600 45.7
4050 50.7
3350 42.5
3450 52.2
3250 45.2
4050 49.3
3800 50.2
3525 45.6
3950 51.9
3650 46.8
3650 45.7
4000 55.8
3400 43.5
3775 49.6
4100 50.8
3775 50.2

Additional Reading

For more information on this topic, see

https://www.geeksforgeeks.org/creating-a-data-frame-from-vectors-in-r-programming/, https://www.statmethods.net/index.html, https://r-charts.com/, http://www.r-tutor.com/, http://www.sthda.com/. (http://www.sthda.com/ is run by the author of ggpubr and has lots of resources for it).

Keywords

  1. palmerpenguins
  2. pander
  3. data.frame