Difference between `do.call()` a function and directly call a function in R? -
here code:
>ipo_num_year<- do.call(length,list(as.name(paste0("all_data_align_",year)))) >ipo_num_year >90 >ipo_num_year<- length(as.name(paste0("all_data_align_",year))) >ipo_num_year >1
year
string object "1999";
in previous code,all_data_align_1999
has been assigned list 90
elements,so right result ipo_num_year
equals 90
.but second line makes ipo_num_year
equals 1
,it means length()
function return value of as.name()
symbol
object,so length 1
.
why return value of as.name()
can not directly used argument of function length()
? , why first solution works fine?
some 1 may ask why don't use length(all_data_align_1999)
.that because year
loop variable in code.
really appreciate kindly reply!
instead of as.name
should use get
:
length(get(paste0("all_data_align_",year)))
you need retrieve object not name.
Comments
Post a Comment