Ragdale:
Assumption: Same body style (e.g. both regular cab) & same drive (e.g. both 2wd manual)
Unfortunately, '02 is a 'cross over' year, so it might be relatively easy, it might not. Check if transmission has the OSS sensor or not. In the '02 I swapped, it had the rear axle VSS, so I was good. If it is has OSS and both are 4WD, keep the 3.0 transfer case and you are good; if this is 2wd, you need some new transmission parts which I am unfamiliar with...
You are going to swap transmission along with engine; no big deal (you planned it that way

)
Easiest way to do this - go to wrecker and get: PATS, PCM, ignition tumbler, door locks and key from '02 Ranger with 4.0 and same transmission (key is the challenge). (I grabbed personality module too, better safe than sorry) Then just swap the 3.0 parts with the wrecker parts and electrical is out of way.
Engine mounts have different part numbers; not sure if they are actually different - I swapped them anyways.
Swap engine, swap transmission and you will have a running '02 with 4.0.