Hello I need help, few days tying to work this out but unable to find examples. "alos would appricate direction on how to find exmples" getting the file name "without the extension" into a character vector. for (i in dir("c:/data/")){ filename <- c(filename,sub("([^.]+)(\\..+)","\\1", i)) } Error: Object "filename" not found and I understand why the error but cann't find a way to fix it. thanks
Fred J. wrote:> Hello > I need help, few days tying to work this out but > unable to find examples. "alos would appricate > direction on how to find exmples" > getting the file name "without the extension" into a > character vector. > for (i in dir("c:/data/")){ > filename <- c(filename,sub("([^.]+)(\\..+)","\\1", > i)) > } > Error: Object "filename" not found > and I understand why the error but cann't find a way > to fix it. > > thanks > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlYou don't need a loop: filename <- sub("([^.]+)(\\..+)", "\\1", dir("c:/data/")) Uwe Ligges PS: Just to get your loop working (you don't want to do it that way!): filename <- NULL for (i in dir("c:/data/")){ filename <- c(filename,sub("([^.]+)(\\..+)","\\1", i))
On Tue, 23 Mar 2004, Fred J. wrote:> Hello > I need help, few days tying to work this out but > unable to find examples. "alos would appricate > direction on how to find exmples" > getting the file name "without the extension" into a > character vector. > for (i in dir("c:/data/")){ > filename <- c(filename,sub("([^.]+)(\\..+)","\\1", > i)) > } > Error: Object "filename" not found > and I understand why the error but cann't find a way > to fix it. >filename <- c() #<--- Initialize. Needed for 1st iterataion. for (i in dir("c:/data/")){ filename <- c(filename,sub("([^.]+)(\\..+)","\\1", i)) } But as noted from another responder -- you don't need a loop. Itay
That's technically an S-PLUS question, more appropriate for S-news than R-help. In any case, see page 31 of `S Programming'. Andy> From: Spencer Graves > > What should one use in place of "sub" in S-Plus, where > "sub" is a > subscripting function? > > Spencer Graves > > Uwe Ligges wrote: > > > Fred J. wrote: > > > >> Hello > >> I need help, few days tying to work this out but > >> unable to find examples. "alos would appricate > >> direction on how to find exmples" > >> getting the file name "without the extension" into a > >> character vector. > >> for (i in dir("c:/data/")){ > >> filename <- c(filename,sub("([^.]+)(\\..+)","\\1", > >> i)) > >> } > >> Error: Object "filename" not found > >> and I understand why the error but cann't find a way > >> to fix it. > >> > >> thanks > >> > >> ______________________________________________ > >> R-help at stat.math.ethz.ch mailing list > >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide! > >> http://www.R-project.org/posting-guide.html > > > > > > You don't need a loop: > > > > filename <- sub("([^.]+)(\\..+)", "\\1", dir("c:/data/")) > > > > > > Uwe Ligges > > > > > > PS: Just to get your loop working (you don't want to do it > that way!): > > > > filename <- NULL > > for (i in dir("c:/data/")){ > > filename <- c(filename,sub("([^.]+)(\\..+)","\\1", i)) > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >