[英]Arrange data by variables of a data.frame in R?
I have written down the following script to get the data in longer format
.我已经写下以下脚本以获取longer format
的数据。 How i can get the data.frame
arrange
by variables
and not by Date
?.我如何让data.frame
按variables
而不是按Date
arrange
?。 That means first i should get the data for Variable A
for all the dates
followed by Variable X
.这意味着首先我应该获取Variable A
后跟Variable X
所有dates
的数据。
library(lubridate)
library(tidyverse)
set.seed(123)
DF <- data.frame(Date = seq(as.Date("1979-01-01"), to = as.Date("1979-12-31"), by = "day"),
A = runif(365,1,10), X = runif(365,5,15)) %>%
pivot_longer(-Date, names_to = "Variables", values_to = "Values")
Maybe I not understood wrigth, but you can arrange your data according to the variables column, through the arrange()
function.也许我不理解 wrigth,但是您可以通过arrange()
函数根据变量列排列数据。
library(tidyverse)
DF <- DF %>%
arrange(Variables)
Resulting this结果这个
# A tibble: 730 x 3
Date Variables Values
<date> <chr> <dbl>
1 1979-01-01 A 3.59
2 1979-01-02 A 8.09
3 1979-01-03 A 4.68
4 1979-01-04 A 8.95
5 1979-01-05 A 9.46
6 1979-01-06 A 1.41
7 1979-01-07 A 5.75
8 1979-01-08 A 9.03
9 1979-01-09 A 5.96
10 1979-01-10 A 5.11
# ... with 720 more rows
在base R
,我们可以使用
DF1 <- DF[order(DF$Variables),]
Am I missing something?我错过了什么吗? This is it.就是这个。
arrange (DF,Variables,Date) %>% select(Variables,everything())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.