How can I manually enter raw data in R?

How can I manually enter raw data in R?

To manually enter raw data in R, you can use the “scan” function. This function allows you to input data directly into R by typing it in the console or copying and pasting it from an external source. The syntax for using the scan function is as follows: “scan(file = “”, what = “”, sep = “”, dec = “”, skip = 0, nmax = -1, flush = FALSE, quote = “””, quiet = FALSE, encoding = “unknown”, fill = FALSE, strip.white = FALSE, blank.lines.skip = TRUE)”. By specifying the appropriate arguments, you can customize the input process to fit your specific data format. This method of manually entering raw data in R can be useful for smaller datasets or when data is not available in a pre-formatted file.

Manually Enter Raw Data in R


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 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 <- c(1, 3, 5, 8, 9)

#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 <- c("Bob", "Mike", "Tony", "Andy")

#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 <- data.frame(team=c("A", "A", "B", "B", "C"),
                 points=c(12, 15, 17, 24, 27),
                 assists=c(4, 7, 7, 8, 12))

#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 <- cbind(points, assists)

#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.

Cite this article

stats writer (2024). How can I manually enter raw data in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-manually-enter-raw-data-in-r/

stats writer. "How can I manually enter raw data in R?." PSYCHOLOGICAL SCALES, 22 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-manually-enter-raw-data-in-r/.

stats writer. "How can I manually enter raw data in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-manually-enter-raw-data-in-r/.

stats writer (2024) 'How can I manually enter raw data in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-manually-enter-raw-data-in-r/.

[1] stats writer, "How can I manually enter raw data in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can I manually enter raw data in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top