代码之家  ›  专栏  ›  技术社区  ›  blackthorne18

如何将R中的数据帧转换为“字符”数据类型?

  •  1
  • blackthorne18  · 技术社区  · 1 年前

    我用于ggplot的labeller函数需要以下类型的数据:

    tempdat <- c(
       "1" = "one",
       "2" = "two"
    )
    

    如果我使用 typeof(tempdat) 它给出一个输出,说明它的类型 character .

    我有一个数据帧,看起来像:

    > tempdf
        parent name
    1       1  one
    2       2  two
    

    我想转换我的数据帧 tempdf 转换为与相同的数据类型 tempdat 我该怎么做?

    2 回复  |  直到 1 年前
        1
  •  3
  •   ThomasIsCoding    1 年前

    你可以试试

    with(tempdf, setNames(name, parent))
    
        2
  •  2
  •   Darren Tsai    1 年前

    您还可以使用 do.call 具有 setNames :

    do.call(setNames, unname(tempdf[2:1]))
    #     1     2 
    # "one" "two"
    

    deframe {tibble} :

    tibble::deframe(tempdf)
    #     1     2 
    # "one" "two"