Please help me to solve the "incompatible types" problem that prevents joining data tables

I have two data tables, and let me suppose them "a" and "infectedvehicle_by_link" respectively.
I used "left_join" function to join them by key value "link_id".
Since it didn't work, I checked their types and the result was as following :
image

Thus, the types of key value that I want to use to match two tables were different. So I coded like followings :

for(i in 1:9) {

a = fread(paste0("exposure_", i, i, ".csv"))
as.character(a)
b = left_join(a, infectedvehicle_by_link, by = ("link_id"))

}

The code returned this error - Error in UseMethod("left_join") : no applicable method for 'left_join' applied to an object of class "character"
So I tried second method...

for(i in 1:9) {

a = fread(paste0("exposure_", i, i, ".csv"))
as.integer(infectedvehicle_by_link)
b = left_join(a, infectedvehicle_by_link, by = ("link_id"))

}

But this returned this error - Error in as.integer(infectedvehicle_by_link) : 'list' object cannot be coerced to type 'integer'

In this situation, what the hell should I do to join two tables?

In both solutions you do too much and too little :wink:

as.character(a)
as.integer(infectedvehicle_by_link)

these are both attempts to convert either an integer to a character or a character to an integer respectively , but , they try to do too much, turn everything in the data.frame, they should specifically only try to alter the join variable i.e. link_id.
also, their output is not stored anywhere. R does not typically modify objects in place, so if you don't assign the result of a calculation i.e. use some_name <- your calc with emphasis on <- then you are likely wasting your cpu cycles, as you will calculate but lose the result to the nameless void...

finally, within each loop , you make a b object , the result of a left_join, but this is not handled elegantly vis the for loop that makes it, each time the for loop makes a new b the old b is overwritten, i.e. you may as well have not looped, but just written code with i set to its last value (9)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.