Instead of as.name
you should use get
:
length(get(paste0("all_data_align_",year)))
You need to retrieve the object not just the name.
Pergunta
Here is my 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
is an string object "1999";
In previous code,all_data_align_1999
has been assigned as an list with 90
elements,so the right result is ipo_num_year
equals to 90
.But the second line makes ipo_num_year
equals to 1
,it means length()
function look the return value of as.name()
as an symbol
object,so its length is just 1
.
Why does the return value of as.name()
can not be directly used as the argument of function length()
?
And why the first solution works fine?
Some one may ask that why don't you just use length(all_data_align_1999)
.That is because the year
is an loop variable in my code.
Really appreciate your kindly reply!
Solução
Instead of as.name
you should use get
:
length(get(paste0("all_data_align_",year)))
You need to retrieve the object not just the name.