6.1 C
London
Thursday, December 19, 2024
HomeRImport & Export Data in RHow to Manually Enter Raw Data in R

How to Manually Enter Raw Data in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

R is one of the most popular programming languages for working with data. But before we can work with data, we have to actually get data into R!

If you already have your data located in a CSV file or Excel file, you can follow the steps in these tutorials to import it into R:

However, sometimes you may want to manually enter raw data into R. This tutorial explains how to do so.

Enter a Vector

We can use the following syntax to enter a single vector of numeric values into R:

#create vector of numeric values
numeric_values #display class of vector
class(numeric_values)

[1] "numeric"
#display vector of numeric values
numeric_values

[1] 1 3 5 8 9

#return second element in vector
numeric_values[4]

[1] 8

We can use the same syntax to enter a vector of character values:

#create vector of character values
char_values #display class of vector
class(char_values)

[1] "character"

Enter a Data Frame 

We can use the following syntax to enter a data frame of a values in R:

#create data frame
df #display data frame
df

  team points assists
1    A     12       4
2    A     15       7
3    B     17       7
4    B     24       8
5    C     27      12

#display class of df
class(df)

[1] "data.frame"

#return value in fourth row and third column
df[4, 3]

[1] 8

Enter a Matrix

We can use the following syntax to enter a matrix of values in R:

#create matrix with two columns and five rows
points=c(12, 15, 17, 24, 27)
assists=c(4, 7, 7, 8, 12)

#column bind the two vectors together to create a matrix
mat #display matrix
mat

     points assists
[1,]     12       4
[2,]     15       7
[3,]     17       7
[4,]     24       8
[5,]     27      12

#display class of mat
class(mat)

[1] "matrix"

#return value in fourth row and second column
mat[4, 2]

assists 
      8

Note: A matrix requires each column to be the same type, unlike data frames.

You can find more R tutorials here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories