rinrin
rinrin

Reputation: 325

Putting rows from old data frame into a new data frame

I have my code:

new_df = data.frame()
#G = 0

for (i in 1:nrow(furin_data)){
  frac = furin_data[i,3]/furin_data[i,5]
  #print(frac)

  if (frac > 2 || frac < 0.5) {
    name = furin_data[i,1]
    print(name)

    new_df = furin_data[i,]
    #print(new_df)
    #G = G + 1
  } 
  write.csv(new_df, "C:\\User\\Documents\\MyData.csv", row.names = FALSE)
}

It creates a new data file, but only the last row is written and not all of the the rows based on the condition. I cannot seem to figure out where is the problem.

Upvotes: 0

Views: 36

Answers (1)

user2332849
user2332849

Reputation: 1450

That's because you're assigning the row to it, so every assignment overrides the previous one. What you want is to add rows to it instead.

new_df[nrow(new_df)+1,] = furin_data[i,]

Another thing is that you created your new_df data frame without any columns, so none are assigned in the transfer. You should define it with the same types and names of columns as furin_data, so those columns could be copied. An easy of initializing it as empty but having the same structure would be:

new_df = furin_data[F,]

Buuut, in the R language, writing a loop is not the best way to do things. R is a vectorized language, meaning it can perform all operations on a vector at once, causing it to execute much much faster. So a conversion of your whole code to R style would be:

library(dplyr)

new_df <-
  furin_data %>%
  mutate(frac = .[3] / .[5]) %>%
  subset(frac > 2 | frac < 0.5)

write.csv(new_df, "C:\\User\\Documents\\MyData.csv", row.names = FALSE)

Upvotes: 2

Related Questions