0

I am subsetting a set of data frames, each data frame will be subsetted into several smaller data frames. Some will produce empty data frames and give an error as in title.

My question: if I ran the script in R console, the script will be executed and new data frames will be generated even with the error messages. but if I use "source" in R studio or try to put the scripts in a function or use a for loop, the scripts will stop running and just display the error message.

Is there a solution? thank you!

user2554330
  • 30,741
  • 4
  • 34
  • 76
Xiao-yan Pan
  • 45
  • 1
  • 1
  • 5
  • 1
    I don't see how we can help you without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – kath May 09 '18 at 10:18
  • You should fix the error. If it doesn't matter to you, just wrap it in `try`. –  May 09 '18 at 10:20

1 Answers1

2

You are not sub-setting your data frame correctly and I would recommend to fix sub-setting instead of trying to ignore the error Here is one example how wrong approach to create a new column can lead to the error you are getting:

df <- data.frame(a = -(1:5), b = 1:5)
df$c[which(df$a>0)] <- 7
#Error in `$<-.data.frame`(`*tmp*`, "c", value = numeric(0)) : 
#  replacement has 0 rows, data has 5

Instead the second statement should be:

rm(df)
df <- data.frame(a = -(1:5), b = 1:5)
df$c[df$a>0] <- 7
df
#    a b  c
# 1 -1 1 NA
# 2 -2 2 NA
# 3 -3 3 NA
# 4 -4 4 NA
# 5 -5 5 NA

To answer your question about catching the error in a script in general, R has try/catch set of functions:

rm(df)
df <- data.frame(a = -(1:5), b = 1:5)
tryCatch({
  df <- data.frame(a = -(1:5), b = 1:5)
  df$c[which(df$a>0)] <- 7
}, error = function(e)print(e)  , warning = function(w) )

You can get more examples of how this function can be used in R documentation and in the Advanced R online book by Hadley Wickham: http://adv-r.had.co.nz/Exceptions-Debugging.html

Katia
  • 3,504
  • 1
  • 13
  • 27
  • Hi Katia, I think fix the subsetting is a good solution. Could you help me further, please? Following your df, I need to subset df1 = df[df$a>3,] add one more column df1$c="method_a". df2=df[df$b>3,] df2$c="method_b" ,df_selected=rbind(df1,df2) I have a several dfs need the same process. But once there is one df1 or df2 is empty, it won't process. Thank you!! – Xiao-yan Pan May 14 '18 at 08:52
  • Hi Katia, I have sorted it out with df$c[df$a>3]="method_a", df$c[df$b>3]="method_b", df_selected=na.omit(df). Thanks a lot! – Xiao-yan Pan May 14 '18 at 09:38