Data Input and Output Flashcards
What are the most basic ways to input data into R?
The most basic functions for data input in R are the following:
c( )
rep( )
seq( ) or :
With these basic functions, we can create objects of more specific classes like:
matrix( )
list( )
data.frame( )
What’s the basic function to input data from keyboard into a vector?
scan( )
How can I read character data from keyboard into vector x?
x = scan(what = “character”)
How can I read the full name of the user from keyboard into a variable called name?
name = readLines(n = 1)
How can I import a csv called “crabs.csv” into a data frame called df, using the two most common ways?
df = read.table(“crabs.csv”, header = TRUE)
or using wrapper function read.csv:
df = read.csv(“crabs.csv”)
Once I’ve loaded my data frame called df, now I want to quickly see the basic structure of the dataframe and its first rows of data, what functions should I use?
str( df )
and
head( df )
Can I use functions read.table and read.csv to read a csv file from a web url?
Yes, I just need to provide the full url to the file argument.
How do I list the data sets available with R installation and how can I load one of them?
data( ) - to list
data( mtcars ) - to load data set mtcards
How can I export my data frame Titanic to a csv?
write.table(Titanic, “titanic.csv”, row.names=F, sep=”,”, dec=”.”)
What function should I use to save a file with text representation of an R object?
dput(da) - to output in the console
dput(da, file = “da.R”) - to write a file
How can I write text representations of three R objects called ma, da, vec into a file? How can I load them into R later?
Write:
dump(c(“ma”, “da”, “vec”), file = “data.R”)
Read:
source(“data.R”)
It creates the objects in your working area with the same names and attributes with which they’ve been stored.
What are this function calls doing? Why is it useful.
save(da, file = “dados.rda”)
save(da, ma, file = “dados.rda”)
save.image(file = “workspace.RData”)
load(“dados.rda”) l
load(“workspace.RData”)
Saving and loading data in binary format. It’s useful when I only have this option or when I need an absolutely great precision to store numbers.
How can I see a list of files present in my working directory?
dir( )
How can I see information about a file called “word_count.ipynb”? What kinds of information does it return?
file.info(“word_counter.ipynb”)
How can I check if file word_cloud.txt is present in the directory?
file.exists(“word_cloud.txt”)
Returns TRUE or FALSE