Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

r - How do i get the row values instead of the row names

I have following code and it doesnt work

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]
)

and instead of typing in r every number like

r = c(1,2,4) 

i want to put in like "df" and it takes the values automatically, but somehow that doesnt happen. Does someone know why?

it should look like this:if r = c(1,2,4)

question from:https://stackoverflow.com/questions/65874787/how-do-i-get-the-row-values-instead-of-the-row-names

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An option is to use a for loop

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

-output enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...