[R] R loops

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Oct 9 14:11:47 CEST 2008


assa.yeroslaviz at bayercropscience.com wrote:
> hello everybody,
>
> I have a directory with over 3000 files with different names. I would like 
> to make some vectors with the file names which are belong together.
>
> I'm trying to do it with a for loop in R:
>
> SF <- c("ad", "cd", "cer", "stress", "salty", "PC", "high", "transfer", 
> "cold", "heat") # the pattern to look for
> names(SF) <- as.vector(SF)
> for (i in 1:length(SF)){
>         write(names(SF)[i], "")
>         for (j in 1:length(SF))
>                 {list[j] <- grep(names(SF)[j], list.files(),value=TRUE) 
> #character vector with the specified stress factor
>                 }
>         }
>
> but all I'm getting is this 
>
> Error in list[j] <- grep(names(SF)[j], list.files(), value = TRUE) : 
>   object of type 'builtin' is not subsettable
>
> I would like the loop to make  in each round a list of the files with the 
> pattern in the directory I'm looking in.
>
> what am i doing wrong?
>   

list is a built-in function.  when you try list[i] (with or without
assignment) without having introduced, within the scope, a subsettable
object named 'list', you got a deserved error message.

otherwise, you'd better use lapply than a for loop.

vQ



More information about the R-help mailing list