Sunday, April 17, 2016

Reading and writing CSV files in R

Before loading a file in R is often useful change directory in the environment, this is done by:
setwd('pathname')
If you have a doubt about which is your current working directory, just print it:
getwd()
Reading from a CSV file to a data frame is pretty simple:
df = read.csv('path/to/file.csv')
Now we can get the structure of the dataframe:
str(df)
It gives us information on the number of observations (rows) and variables (columns); names of variables, a few of their values and, when they are detected as 'factors', also the number of 'level'on which that variable is structured.
Another useful function is:
summary(df)
It tries to provide us useful summary for each variable, giving the levels in case of factor, or a few statistic measures otherwise (min, max, mean, median, first and third quartile).

We can create a subset from a dataframe selecting a specific value for a variable, like this:
sub = subset(df, MyVariable = 'a value')
Then we can save this subset to a CSV file:
write.csv(sub, 'path/to/subFile.csv')

No comments:

Post a Comment