Search code examples
rloopsr-rio

How can i loop this function in R?


Basically i have two dataframes and i want to transfer the attributes of each variable of one to the other dataframe that has the same variables but with null attributes (none). Let me know thnks

attributes(egresados$q0001)<-attributes(egresados_atr$q0001)
attributes(egresados$q0002)<-attributes(egresados_atr$q0002)
attributes(egresados$q0003)<-attributes(egresados_atr$q0003)
attributes(egresados$q0004)<-attributes(egresados_atr$q0004)
attributes(egresados$q0005_0001)<-attributes(egresados_atr$q0005_0001)
attributes(egresados$q0005_0002)<-attributes(egresados_atr$q0005_0002)
attributes(egresados$q0005_0003)<-attributes(egresados_atr$q0005_0003)
attributes(egresados$q0005_0004)<-attributes(egresados_atr$q0005_0004)
attributes(egresados$q0005_0005)<-attributes(egresados_atr$q0005_0005)
attributes(egresados$q0005_0006)<-attributes(egresados_atr$q0005_0006)
attributes(egresados$q0005_0007)<-attributes(egresados_atr$q0005_0007)
attributes(egresados$q0006_0001)<-attributes(egresados_atr$q0006_0001)
attributes(egresados$q0006_0002)<-attributes(egresados_atr$q0006_0002)
attributes(egresados$q0006_0003)<-attributes(egresados_atr$q0006_0003)
attributes(egresados$q0006_0004)<-attributes(egresados_atr$q0006_0004)
attributes(egresados$q0007)<-attributes(egresados_atr$q0007)
attributes(egresados$q0008)<-attributes(egresados_atr$q0008)
attributes(egresados$q0009_0001)<-attributes(egresados_atr$q0009_0001)
attributes(egresados$q0009_0002)<-attributes(egresados_atr$q0009_0002)
attributes(egresados$q0009_0003)<-attributes(egresados_atr$q0009_0003)
attributes(egresados$q0009_0004)<-attributes(egresados_atr$q0009_0004)
attributes(egresados$q0010_0001)<-attributes(egresados_atr$q0010_0001)
attributes(egresados$q0011_0001)<-attributes(egresados_atr$q0011_0001)
attributes(egresados$q0012)<-attributes(egresados_atr$q0012)
attributes(egresados$q0013_0001)<-attributes(egresados_atr$q0013_0001)
attributes(egresados$q0014)<-attributes(egresados_atr$q0014)

I'm using tidyverse and rio packages in my stuggle and i know the functions gather_attrs and spread_attrs i think may help but yet don't know how....


Solution

  • You could do this with a for loop on names (no need to rely on names being in the same order).

    for (s in names(egresados)) {
      attributes(egresados[[s]]) <- attributes(egresados_atr[[s]])
    }