【问题标题】:How do i get the row values instead of the row names我如何获取行值而不是行名
【发布时间】:2021-01-24 19:11:27
【问题描述】:

我有以下代码,但它不起作用

df <- data.frame (
  Bikes = c("Canyon", "Santa", "Radon"),
  Tretlage = c(1,1,3),
  Kurbel = c(2,3,4),
  Winkel = c(4,5,3)
)


plot_ly(
      type = 'scatterpolar',
      mode = "closest",
      fill = 'toself'
    ) %>%
      add_trace(
        r = df[1,],
        theta = c("Tretlage", "Kurbel","Winkel"),
        showlegend = TRUE,
        mode = "markers",
        name = df[1,1]
)

而不是像每个数字一样输入 r

r = c(1,2,4) 

我想像“df”一样输入它并自动获取值,但不知何故这不会发生。有人知道为什么吗?

应该是这样的:if r = c(1,2,4)

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    一种选择是使用for 循环

    p1 <- plot_ly(
      type = 'scatterpolar',
      mode = "closest",
      fill = 'toself'
    )
    
    for(i in seq_len(nrow(df))) {
         p1 <- p1  %>%
                 add_trace(
               r = unlist(df[i,-1]),
               theta = c("Tretlage", "Kurbel","Winkel"),
            showlegend = TRUE,
            mode = "markers",
            name = df[i,1]
          )
      }
    
    p1
    

    -输出

    【讨论】:

    • 谢谢,但是我该如何绘制呢?我想要这个数字,但像这样分配给 p1。
    • @AndreaBin 只需输入“p1”并在您的控制台上输入
    • 哦,谢谢,但遗憾的是它仍然遇到同样的问题。而不是值 1,2,4 将 0,1,2 分配给 r。
    • @AndreaBin 我认为您的索引是错误的,即r = df[i,] 还包括第一列。你可能需要r = unlist(df[i,-1])
    • 它应该是这样的:i.stack.imgur.com/rbtb6.jpg
    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多