Tuesday, September 21, 2010

R: Generic Data Input / Installing Libraries

# Usually, the easiest way to input data (from MS Excel or most other GUI spreadsheets) is to save your data text file (usually tab-delimited or comma-separated values), then have R read it in.

# Red indicates stuff you gotta change.

# For data in a TAB-DELIMITED form:
infile <- read.delim("C:/...",header=TRUE) # where "C:/..." is the path of the file you want to load.

# For data in a comma-seperated form:
infile <- read.csv("C:/...",header=TRUE)# where "C:/..." is the path of the file you want to load.


# To pick out your variables of interest and put them in a data frame(not always necessary):
VarsOfInt <- data.frame(infile$Var1,infile$Var2,infile$Var3,...,infile$Var)


# NOTE: Input files do not have to be called infile - you can call it whatever you want. But if you do, you'll have to make sure you change any subsequent infile you see to match whatever you called the file you read in.

# To install libraries:

install.packages("name_of_library") # OR

install.packages("name_of_library",dependencies=TRUE) # if you want all the dependencies installed as well - this might prevent future problems but might also take all day.

No comments:

Post a Comment