[R] Please help me debugg

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Fri Nov 5 09:24:22 CET 2021


On Fri, 5 Nov 2021 12:29:21 +0530
Shrinivas Dharma <dharma using metricuk.com> wrote:

> I am trying to run the following double loop which  I have written
> but it is not running.

Thanks for providing the code, but it's much easier to help you if you
provide the data, too. Use dput(head(...)) to make it easy for us to
copy-paste the data.

> Unable to get my head around the mistake. Please try to spot the bug
> and help

> > for(i in 1:122){  
> + for(j in 1:68){
> + if(V(FriendGraph)$name[i]==names[j]){
> + gender_vector[i]<-sex[j]
> + break;}
> + else{gender_vector[i]<-NA}
> + } }

> Error in `[.data.frame`(sex, j) : undefined columns selected

sex[j] extracts the `j`th column out of the `sex` data frame. Does
`sex` contain 68 columns? If you want the `j`th row, use sex[j,].

Also, it may help to use merge() and have the R language do some of the
work for you, if you organise your `names` and `sex` into a single
data.frame to merge V(FriendGraph) with.

Additionally, you might save some of the execution time if you move the
V(FriendGraph) calculation out of the loop.

> 	[[alternative HTML version deleted]]

P.S. Your post came across mostly fine, but please compose your mail in
plain text, not HTML. We only see the plain text version, and in many
cases, the mailers horribly mangle the post when they automatically
generate it from the HTML message.

-- 
Best regards,
Ivan



More information about the R-help mailing list