旋转较长的前缀更长

发布于 2025-01-29 14:05:55 字数 1738 浏览 3 评论 0原文

我必须使此DataFrame mencem_moran 更长的时间才能进行可视化。

我基本上有一个常见的列距离,然后是16个变量的MoranIndex和Zscore(社区,CSRSTRATEGY, + 14),

我试图更长的时间与下面的代码相关。问题是 我不能将支柱作为第三列(必不可少的,因为它将有助于区分观测值),而单列具有价值。

try1 <- Increm_Moran %>%
  pivot_longer(
    cols = -Distance, 
    names_to = "Pillar",
    values_to = "Value")

try2 <- Increm_Moran %>%
  pivot_longer(
    cols = -Distance,
    names_to = c(".value", "Pillar"),
    names_sep = "_", 
    values_drop_na = TRUE)

“第二次尝试”

第二次尝试似乎与我更相关。仅缺少两列:

  • 另外一列带有数值数据,
  • 另一列带有支柱的名称(社区,CSRSTRATTRETATEGY等)。
价值支柱
12.1社区
12.34社区
43.43CSRSTRATEGY
34.34CSRSTRATTRATEGY

枢纽会使头痛非常快,如果您有任何建议,非常感谢您

I have to make this dataframe Increm_Moran longer in order to make visualisations.

enter image description here

I have basically a common column distance, and then MoranIndex and zscore for 16 variables (community, csrstrategy, + 14 more)

I have tried to pivot longer with code below. Problem is
I can't have Pillar as a third column (essential because it will help differentiate observations), and single column with value.

try1 <- Increm_Moran %>%
  pivot_longer(
    cols = -Distance, 
    names_to = "Pillar",
    values_to = "Value")

try2 <- Increm_Moran %>%
  pivot_longer(
    cols = -Distance,
    names_to = c(".value", "Pillar"),
    names_sep = "_", 
    values_drop_na = TRUE)

First try

Second try

The second attempt seems more relevant to me. Just two columns are missing :

  • one more column value with numerical data,
  • another with the name of pillar (community, csrstrategy, etc).
ValuePillar
12.1community
12.34community
43.43csrstrategy
34.34csrstrategy

Pivoting gives headaches really fast, if you have any advice, thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2025-02-05 14:05:55

我在写这篇文章时找到了答案。
我给出答案

    temp <- pivot_longer(Increm_Moran, -Distance, 
names_sep = "\\_", names_to = c("Pillar", "variable"))

如果这可以帮助其他人和输出,

I found my answer while writing this post.
I give the answer if this can help other people

    temp <- pivot_longer(Increm_Moran, -Distance, 
names_sep = "\\_", names_to = c("Pillar", "variable"))

and the output :

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文