[R] Hi
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sat Dec 17 19:39:13 CET 2011
On 14.12.2011 23:11, Trying To learn again wrote:
> Hi all,
>
> I have solved my question
>
>
> Output<- matrix(0, length(x), 1)
>
>
> for (i in 1:length(x)){
>
> files<- paste('KT', i, '.csv', sep = '')
>
>
>
> da1<- read.csv(files)
>
> Output[i,1]<-da1[1,2]
> }
2 comments:
1: use seq:_along(x) rather than 1:length(x) to protect against length 0
objects.
2: use list.files(pattern="^KT.*\\.csv$") to get all the files you were
looking for from the current directory, hence no need to construct the
filenames.
That way it is probably a one-liner (untested):
Output <- sapply(list.files(pattern="^KT.*\\.csv$"), function(x)
read.csv(x)[1,2])
Uwe Ligges
>
> 2011/12/14 Trying To learn again<tryingtolearnagain at gmail.com>
>
>> Hi all,
>>
>> I have 100 csv files always with this information (I Attach two example
>> excels)
>>
>> KT80.csv contains:
>> ,"x"
>> 1,127.188271604938
>>
>> KT80.csv contains
>>
>> ,"x"
>> 1,1.06329287351545
>>
>>
>> I have three questions:
>>
>> First
>>
>> When I created this input files I used write.csv and it authomaticaly
>> creates the structure of the file as you can see.
>>
>> Second. If I try to read csv it appears:
>>
>> read.csv(KT2.csv,header=T, sep='.')
>> Error en read.table(file = file, header = header, sep = sep, quote =
>> quote, :
>> objeto 'KT2.csv' no encontrado
>>
>> But the file is in the right directy....
>>
>> Third (supossing I can solve the format files)
>>
>> My main question is:
>>
>> I want to create a matrix (or data frame) for instance from file KT80.csv
>> to KT81.csv so that the rows are without header and position.
>>
>>
>>
>> 127.18
>> 1.06
>>
>>
>> Wich function would help me?
>>
>> I think I should use something like
>>
>>
>>
>> ffor (i in 80:81){
>>
>> files<- paste('KT', i, '.csv', sep = '')
>>
>> myfun<- function(d) {
>>
>> da1<- read.csv(d)
>>
>> Output[i,1]=da1[1,2]
>>
>>
>>
>> }
>>
>> lout<- lapply(files, myfun)
>>
>> }
>>
>> But results that says Output doesn´t exists?
>>
>> I must create before? How?
>>
>> Many Thans¡¡¡
>>
>
> [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list