Run setblock command in Minecraft to change villager into pig

Solution 1:

It is not possible to directly convert an entity into a different entity. The best workaround is to summon a pig at the villager and kill the villager.

Like so:

execute as @e[type=villager,tag=villager1] at @s run function namespace:convert_to_pig

function convert_to_pig:

summon pig ~ ~ ~
 
tp @s ~ -500 ~
kill @s