If you are working in industry or academic research you will be working with data stored neatly (hopefully) in files. So it is highly desired to learn how to get data from a file into R. The idea is to get all the data from the file very carefully into an R object and from there we are in our home territory.
#this line below reads the csv straight into a data frame called worms
worms<-read.csv("worms.csv")
#this line below prints the head (or top) of the data frame
head(worms)
## Field.Name Area Slope Vegetation Soil.pH Damp Worm.density
## 1 Nashs.Field 3.6 11 Grassland 4.1 FALSE 4
## 2 Silwood.Bottom 5.1 2 Arable 5.2 FALSE 7
## 3 Nursery.Field 2.8 3 Grassland 4.3 FALSE 2
## 4 Rush.Meadow 2.4 5 Meadow 4.9 TRUE 5
## 5 Gunness.Thicket 3.8 0 Scrub 4.2 FALSE 6
## 6 Oak.Mead 3.1 2 Grassland 3.9 FALSE 2
To save a file, it is a very easy:
# just provide the object and the file name
write.csv(worms,"new_worms.csv")
There are plethora of inbuilt R functions, that will help you read and write files. In the help window of RStudio type read
or write
and press enter to check what is available.
Introduction to R by Dr. Sarath Chandra Dantu
This course material is available under a Creative Commons BY-SA license (CC BY-SA) version 4.0