Tuesday, October 26, 2010

When you can't find your file on R

I think the worst thing about scripting is trying to import files.

This of course is immediately glossed over by all help manuals, because it's hard to do and I think everyone finds their little way and holds onto it like a programming baby.

Well, I will share the easiest way. I think mapping a drive is too complex and you spend so much time trying to put the right number and kind of slashes, so I say do this.

R's prompt will say

>>

And of course you type something.

Let's say you want a variable called "biomass" and it's going to be a data set in a part of your computer that's really obscure like "...\temporary\data\apps\01ab151974".  The first thing you must do is open that file in Excel and save it as either .txt or .csv. Save its somewhere easy to navigate to, like YOUR DESKTOP. Just save a copy there for now, ok.
Now, at the prompt (>>) type:

biomass = read.csv (file = file.choose())

You will get a little navigating window to come up and you can just go to your desktop, find that file, and bam! It's in!

If you can't seem to use the file, it probably means R decided to interpret it as a "list" instead of a data frame or matrix that you want. So you will want to do this. Save it as "biomass1" (so that your original doesn't get lost) and type the following converstions at the command prompt (>>)
FOR A DATA FRAME:
biomass1=as.data.frame(biomass)

or

FOR A MATRIX:
biomass1 = as.matrix(biomass)

Okay! That's my daily tip!