我如何获取行值而不是行名
How do i get the row values instead of the row names
我有以下代码,但它不起作用
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)
一个选项是使用 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
-输出
我有以下代码,但它不起作用
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]
)
而不是像
这样每个数字都输入 rr = c(1,2,4)
我想像“df”这样输入,它会自动获取值,但不知何故这并没有发生。有人知道为什么吗?
它应该是这样的:if r = c(1,2,4)
一个选项是使用 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
-输出