这是当前的数据帧结构
public class Banana : Fruit{
public string peelDensity { get; set; }
//this is a sample of Default Constructor
public Banana()
:base()
{
Price = 0; //here you're setting default value to banana once it's initialized
}
public override void GetPrice()
{
//base.GetPrice(); //here you get the value from the Parent Class
Price = 100; // here you're setting a value for the price of Banana once you call this method;
}
}
想要将其更改为
value. blue red
1 -1.22049503 -0.62733486
2 1.61641224 0.43102051
3 0.09079087 0.61619844
4 0.32325956 -0.17718356
我看到了另一个问题的问题。 Transform structure of data frame in R
答案 0 :(得分:1)
tidyr::gather
可以做到。假设数据帧被命名为df1
:
library(tidyr)
df1 %>%
gather(color, number, -value.)
如果您希望示例中的结果按value.
排序,请添加dplyr::arrange
:
library(tidyr)
library(dplyr)
df1 %>%
gather(color, number, -value.) %>%
arrange(value.)
答案 1 :(得分:0)
@ d.b的评论和@neilfws的回答很棒。这是另一种方式:
library(dplyr)
df <- data.frame(
value = c(1, 2, 3),
blue = c(10, 30, 50),
red = c(20, 40, 60)
)
df2 <- data.frame(
value = rep(1:nrow(df), each = 2),
color = rep(c("blue", "red"), nrow(df))
) %>%
mutate(number = if_else(color == "blue", df$blue[value], df$red[value]))