Create a Table Manually

You want to create a data frame by typing in each value by hand. This is an alternative to reading a file that contains the data.

Step 1 - Call tibble::tibble(). tibble() constructs a tibble, a type of data frame.

Step 2 - Choose a column name for your tibble. Pass the name as an argument name to tibble(), e.g.

tibble(col_1)

Step 3 - Provide a vector of values for the column. Assign them to the column name, e.g.

tibble(col_1 = c("a", "b", "c"))

Step 4 - Repeat for every column in your tibble. Every column should have the same number of values. If you pass a column a single value, tibble() will repeat that value for each row of the data frame.

Remember to separate each new argument/column name with a comma.

Step 5 - Save the tibble to an object, so you can access it later.

data <- tibble(
  col_1 = c("a", "b", "c"),
  col_2 = 1:3
)

Example

We want to create a data frame to keep track of:

  1. The teachers at Grove Middle School
  2. The number of students in their classrooms
  3. The grade levels of the students

We begin by loading the tibble package which contains tibble(). Next we input our data into tibble(). Our tibble will have the column names teacher, class_size, and grade with the values provided below.

library(tibble)
tibble(
  teacher = c("Gaines", "Johnson", "Hernandez"),
  class_size = c(30, 26, 28),
  grade = c(6, 7, 8)
)
# A tibble: 3 × 3
  teacher   class_size grade
  <chr>          <dbl> <dbl>
1 Gaines            30     6
2 Johnson           26     7
3 Hernandez         28     8

After checking out the created tibble, we realize it would be a good idea to include the school name in the data frame in case we decide to include other schools in the future. If we pass only a single value to this column, R will use its recycling rules to reuse that value for each row in the tibble.

tibble(
  teacher = c("Gaines", "Johnson", "Hernandez"),
  class_size = c(30, 26, 28),
  grade = c(6, 7, 8),
  school = "Grove MS"
)
# A tibble: 3 × 4
  teacher   class_size grade school  
  <chr>          <dbl> <dbl> <chr>   
1 Gaines            30     6 Grove MS
2 Johnson           26     7 Grove MS
3 Hernandez         28     8 Grove MS

Lastly, we assign the created tibble to the object, teachers, so we can access is later.

teachers <- tibble(
  teacher = c("Gaines", "Johnson", "Hernandez"),
  class_size = c(30, 26, 28),
  grade = c(6, 7, 8),
  school = "Grove MS"
)

tribble()

You can also define your tibble row by row with tibble:tribble(). Place a ~ before each value in the first row to indicate that the values are column names. If you watch your spaces, tribble() will provide a low-fi preview of your table as you write it.

tribble(
   ~teacher, ~class_size, ~grade,    ~school,
   "Gaines",          30,      6, "Grove MS",
  "Johnson",          26,      7, "Grove MS",
"Hernandez",          28,      8, "Grove MS"
)
# A tibble: 3 × 4
  teacher   class_size grade school  
  <chr>          <dbl> <dbl> <chr>   
1 Gaines            30     6 Grove MS
2 Johnson           26     7 Grove MS
3 Hernandez         28     8 Grove MS

Other ways to read in raw data

Any of the readr::read_* functions, such as readr::read_csv() or readr::read_delim(), can be used to create a table manually as a character string wrapped in I().

readr::read_csv(I("col_1,col_2\na,1\nb,2\nc,3"))
Rows: 3 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): col_1
dbl (1): col_2

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 3 × 2
  col_1 col_2
  <chr> <dbl>
1 a         1
2 b         2
3 c         3

tribble() and SAS

tribble() is most similar to typing raw data directly into your SAS’s DATALINES statement in the DATA step to indicate internal data.

In SAS, you must specify the data type following each column name in the INPUT statement, whereas tribble() automatically chooses data types.

In SAS:

DATA data_new;
  INPUT col_1 $ col_2;
  DATALINES;
a 1
b 2
c 3
  ;
run;

In R:

data_new <- tribble(
  ~col_1, ~col2,
     "a",     1,
     "b",     2,
     "c",     3
)