[R] condense repetitive code for read.csv and rename.vars
Crombie, Burnette N
bcrombie at utk.edu
Thu Aug 15 17:07:56 CEST 2013
Thanks very much for your contribution, Siraaj. I appreciate you taking the time to help me learn loops, etc. BNC
-----Original Message-----
From: Siraaj Khandkar [mailto:siraaj at khandkar.net]
Sent: Wednesday, August 14, 2013 9:08 PM
To: Crombie, Burnette N
Cc: r-help at r-project.org
Subject: Re: [R] condense repetitive code for read.csv and rename.vars
On 08/14/2013 03:43 PM, bcrombie wrote:
> Is there a more concise way to write the following code?
>
> library(gdata)
> mydataOUTPUTrtfA <- read.csv("mergedStatstA.csv")
> save(mydataOUTPUTrtfA, file="mydataOUTPUTrtfA.RData") mydataOUTPUTrtfA
> <- rename.vars(mydataOUTPUTrtfA, from="X", to="Statistics.Calculated",
> info=FALSE)
>
> mydataOUTPUTrtfB <- read.csv("mergedStatstB.csv")
> save(mydataOUTPUTrtfB, file="mydataOUTPUTrtfB.RData") mydataOUTPUTrtfB
> <- rename.vars(mydataOUTPUTrtfB, from="X", to="Statistics.Calculated",
> info=FALSE)
>
> mydataOUTPUTrtfC <- read.csv("mergedStatstC.csv")
> save(mydataOUTPUTrtfC, file="mydataOUTPUTrtfC.RData") mydataOUTPUTrtfC
> <- rename.vars(mydataOUTPUTrtfC, from="X", to="Statistics.Calculated",
> info=FALSE)
>
> I will have a series of mydataOUTPUTrtf files spanning a large portion
> of the alphabet, so to speak:
> e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help
>
alphabet <- c("FOO", "BAR", "BAZ")
for (a in alphabet) {
filename <- paste(c("basename", a, ".csv"), collapse="")
data <- read.csv(filename)
date <- rename.vars( data
, from="X"
, to="Statistics.Calculated"
, info=FALSE
)
# do some other stuff with data
}
You should be able to pick it up from here.
In case you need an actual alphabet, it is already predefined:
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
[18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
>
More information about the R-help
mailing list