[R] searching through two different texts files to find a common variable

Daniel Malter daniel at umd.edu
Mon Jun 20 22:10:25 CEST 2011


You probably want to use the merge() function. 

imaginary example for data frames named "KS" and "US," where the common
identifier variable is named "ID"

merged.data<-merge(KS,US,by.x="ID",by.y="ID",all.x=F,all.y=F)

Note that this will retain only observations for which there is a common ID
in both KS and US. If you want to keep observations that do not have
corresponding ID in the other dataset, you would have to adjust the all.x
and/or the all.y argument.

HTH,
Daniel




casswilson6 wrote:
> 
> I have two different text files one for the whole USA and one for KS.  The
> USA files contains stations numbers with the corresponding latitudes and
> longititudes. The KS file contains all the data I need to maniuplate. But
> I need to connect the latitude and longitude given in the US file to the
> KS file. They are connected by station number.
> I am trying to use a which function to sort through the two data sets
> finding where ST_ID (USA file) is equal to the ID ( KS file) and finding
> the latitudes where this is true. 
> 
>> xlat <- lat[which(ST_ID == ID, arr.ind == FALSE)]
> Error in which(ST_ID == ID, arr.ind == FALSE) : 
>   object 'arr.ind' not found
> In addition: Warning message:
> In ST_ID == ID :
>   longer object length is not a multiple of shorter object length
> 
> Obviously the USA file is longer and I need to ignore the length
> differences some how...???
> 
> Thanks,
> Cassie
> 

--
View this message in context: http://r.789695.n4.nabble.com/searching-through-two-different-texts-files-to-find-a-common-variable-tp3612101p3612408.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list