【问题标题】:How can i loop this function in R?我如何在 R 中循环这个函数?
【发布时间】:2021-07-06 05:01:04
【问题描述】:

基本上我有两个数据帧,我想将一个数据帧的每个变量的属性转移到另一个具有相同变量但具有空属性(无)的数据帧。让我知道谢谢

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)

我在 stuggle 中使用 tidyverse 和 rio 包,我知道函数 gather_attrsspread_attrs 我认为可能会有所帮助,但不知道如何......

【问题讨论】:

    标签: r loops rio


    【解决方案1】:

    您可以对名称使用 for 循环来执行此操作(无需依赖名称的顺序相同)。

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

    【讨论】:

    • 天才。我已经被困了一个月,有时最好相信专家。虽然在开始时我运行该函数并且我的 df egresados 变成了一个列表,然后我再次重新运行该函数并且它起作用了。最良好的祝愿让 - 克劳德
    【解决方案2】:

    您可以使用Map 尝试以下操作-

    egresados[] <- Map(function(x, y) {attributes(x) <- attributes(y);x}, 
        egresados, egresados_atr[names(egresados)])
    

    【讨论】:

    • 您好 Ronak 感谢您的快速回复。我发现您的解决方案并不能解决我的问题,因为它将我的数据框(df)“egresados”变成了一个列表。让我提供更多信息来解决问题。我有这个 df 1: colnames(prueba1) [1] "CollectorNm" "q0001" "q0002" "q0002_other" 和 df2 具有相同的变量。 attributes(prueba1$q0001) 是:$label [1] "Hecha esta aclaración, ¿desea continuar con la encuesta?" $format.spss [1] "F8.2" $labels Sí No 1 2 我如何将每个变量属性传递给 df2 的变量,而不需要为每个变量重复上面的内容
    • 我在答案中添加了[] 以保留数据框结构。这有帮助吗?
    【解决方案3】:

    假设它们按相同的顺序排序,那么您可以使用以下内容:

    # create some data as an example
    egresados <- setNames(as.list(1:14), sprintf("q%04d", 1:14))
    egresados_atr <- setNames(as.list(letters[1:14]), sprintf("q%04d", 1:14))
    for(i in 1:length(egresados_atr))
      attributes(egresados_atr[[i]]) <- list(value = egresados_atr[[i]], 
                                             x = 1)
    
    # get the result
    res_1 <- Map(
      function(value, other) do.call(structure, c(list(value), attributes(other))), 
      value = egresados, other = egresados_atr)
    str(head(res_1, 3)) # show the result for the first three values
    #R> List of 3
    #R>  $ q0001: int 1
    #R>   ..- attr(*, "value")= chr "a"
    #R>   ..- attr(*, "x")= num 1
    #R>  $ q0002: int 2
    #R>   ..- attr(*, "value")= chr "b"
    #R>   ..- attr(*, "x")= num 1
    #R>  $ q0003: int 3
    #R>   ..- attr(*, "value")= chr "c"
    #R>   ..- attr(*, "x")= num 1
    
    # in R 4.1.0 or greater                   
    res_2 <- Map(c, lapply(egresados, list), lapply(egresados_atr, attributes)) |> 
      lapply(do.call, what = structure)
    
    # they give the same
    all.equal(res_1, res_2)
    #R> [1] TRUE
    

    第一个答案虽然非常接近Ronak Shah's answer,但使用了do.callstructure。您可以使用egresados_atr[names(egresados)],因为他指出如果数据未按相同顺序排序或元素数量不相等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2021-02-16
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多