0

I am reading multiple txt files and I would like to remove the first row and save it as a variable, transpose it and later when I'm done doing stuff I would like to save it along with other variables in a file.

The problem is that my code is just saving the first txt information and repeating it for the rest of the txt files.

My code:

    for(i in 1:length(filesW))
{
  data <- read.table(paste0("data",i,".txt", sep=""), sep=",", header=TRUE)
  winstepsdat2 <- data.frame(data)


  genparameter<-t((winstepsdat2[1,]))
  winstepsdat<-winstepsdat2[-1,]

  num_col<-ncol(winstepsdat)
  num_row<-nrow(winstepsdat)

  colnames(winstepsdat) <- paste("i", 1:num_col, sep="")
  winstepsdat$name<- paste ("p", 1:num_row, sep="")

  #must change ni and labels for 1:n??
  cmd <- wcmd(title = "R2Winsteps Example", data=paste0("data",i,".txt"),item1 = 1, ni =num_col , name1 = 30, namelen = 20,labels = paste('i',                                                                                                                                1:num_col, sep = ""), hlines = "Y" ) #codes=1:5

  write.wdat(winstepsdat, cmd)

  write.wcmd(cmd, paste0("CMFILE[",i,"].cmd"))

  winsteps2(cmd, outfile=paste0("outfile[",i,"].txt"), pfile=paste0("pfile",i,".txt"), ifile=paste0("ifile",i,".txt"), windir="C:/Winsteps/Winsteps.exe")

} #end for

Somehow winstepsdat does change as I go through text files (given that I get different values for other calculations, but genparameter just keeps the first txt file values. How can I store/save genparameter for every text file I read?

I'm a beginner programmer so I would appreciate the help. Thanks, Shmy

0

1 Answer 1

1

I don't know if I understand your question correctly, but I am guessing that what you need is to initialize the variables (for instance, as lists), and then fill the list with the for loop.

Something like:

genparameter = list()
winstepsdat = list()
for(i in 1:length(filesW)) #i don't know what the length(filesW) was doing here
{
 data <- read.table(paste0("data",i,".txt", sep=""), sep=",", header=TRUE)
winstepsdat2 <- data.frame(data)

genparameter[[i]]<-t((winstepsdat2[1,])
winstepsdat[[i]] <-winstepsdat2[-1,]

...
Sign up to request clarification or add additional context in comments.

7 Comments

I don't think this is what OP is trying to do, but I do think it's what OP should do :)
@Eudald, thank you but sadly it didn't work. When I read the first text file the values do not get updated as the loop continues through the next text file. length (filesW) goes through my files that's how long I want the loop to me :)
Mmmm, I see... Can you give me the whole function and what you expect it to do? Then I might understand better what the problem is.
@Eudald, Yes, I updated my post with the whole code (because the comment section warned me I had too many characters). Basically, I don't know why the loop doesn't go beyond the first text file. Thank you for trying!
What is filesW? Have you checked that its length exists and it's bigger than 1? (I know it sounds stupid but these kind of things often happen to me...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.