我有這個資料框:
structure(list(Segment.Number = c("Start Event", "Start Time",
"End Event", "End Time", "Segment Duration", "Total SCRs", "ER-SCRs",
"NS-SCRs", "Tonic SCL", "Mean SC", "Tonic Period"), X1 = c("time.txt:start pressed (F1):BL1",
"60", "time.txt:start pressed (F1):BL2", "200", "140", "27",
"0", "27", "16.877020827457564", "17.325101513639225", "80.45693848354793"
), X2 = c("time.txt:start pressed (F1):F1", "215", "time.txt:start pressed (F1):F2",
"515", "300", "68", "1", "67", "18.507943774797333", "19.012163892375462",
"165.33022014676453"), X3 = c("time.txt:start pressed (F1):W1",
"2040", "time.txt:start pressed (F1):W2", "2940", "900", "155",
"0", "155", "22.1224503921822", "22.600699854723032", "546.20937986219167"
), Path = c("Code1", "Code1", "Code1", "Code1", "Code1", "Code1",
"Code1", "Code1", "Code1", "Code1", "Code1")), row.names = c(NA,
-11L), class = "data.frame")
我想改變它的格式,所以看起來像這樣:
“Segment.Number”列下的值將是列名,每個值都在正確的位置。
我已經完成了很多 Gather 和 Spread 功能,但我無法做到正確。
我已經查看了類似的問題,但這里有些不同,我還需要將值轉換為列。
非常感謝,伊多
uj5u.com熱心網友回復:
函式spread
和gather
被棄用,取而代之的是pivot_wider
和pivot_longer
。首先將資料規范化為 3NF/tidy 形式,使用pivot_longer
. 然后,您只有名稱(鍵)、值對,可以使用以下方法將其放入多個新列中pivot_wider
:
library(tidyverse)
data <- structure(list(Segment.Number = c(
"Start Event", "Start Time",
"End Event", "End Time", "Segment Duration", "Total SCRs", "ER-SCRs",
"NS-SCRs", "Tonic SCL", "Mean SC", "Tonic Period"
), X1 = c(
"time.txt:start pressed (F1):BL1",
"60", "time.txt:start pressed (F1):BL2", "200", "140", "27",
"0", "27", "16.877020827457564", "17.325101513639225", "80.45693848354793"
), X2 = c(
"time.txt:start pressed (F1):F1", "215", "time.txt:start pressed (F1):F2",
"515", "300", "68", "1", "67", "18.507943774797333", "19.012163892375462",
"165.33022014676453"
), X3 = c(
"time.txt:start pressed (F1):W1",
"2040", "time.txt:start pressed (F1):W2", "2940", "900", "155",
"0", "155", "22.1224503921822", "22.600699854723032", "546.20937986219167"
), Path = c(
"Code1", "Code1", "Code1", "Code1", "Code1", "Code1",
"Code1", "Code1", "Code1", "Code1", "Code1"
)), row.names = c(
NA,
-11L
), class = "data.frame")
data %>%
as_tibble() %>%
# always normalize the data first
pivot_longer(c(X1, X2, X3), names_to = "Time") %>%
# format to desired shape
pivot_wider(names_from = Segment.Number)
#> # A tibble: 3 × 13
#> Path Time `Start Event` `Start Time` `End Event` `End Time` `Segment Durat…`
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 Code1 X1 time.txt:sta… 60 time.txt:s… 200 140
#> 2 Code1 X2 time.txt:sta… 215 time.txt:s… 515 300
#> 3 Code1 X3 time.txt:sta… 2040 time.txt:s… 2940 900
#> # … with 6 more variables: `Total SCRs` <chr>, `ER-SCRs` <chr>,
#> # `NS-SCRs` <chr>, `Tonic SCL` <chr>, `Mean SC` <chr>, `Tonic Period` <chr>
由reprex 包于 2022-06-28 創建 (v2.0.0 )
uj5u.com熱心網友回復:
我們可以將 pivot_longer 與 pivot_wider 結合起來:
library(dplyr)
library(tidyr)
df %>%
pivot_longer(-Segment.Number) %>%
pivot_wider(names_from=Segment.Number, values_from=value)
name `Start Event` `Start Time` `End Event` `End Time` `Segment Durat…` `Total SCRs` `ER-SCRs` `NS-SCRs` `Tonic SCL` `Mean SC` `Tonic Period`
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 X1 time.txt:start… 60 time.txt:s… 200 140 27 0 27 16.8770208… 17.32510… 80.4569384835…
2 X2 time.txt:start… 215 time.txt:s… 515 300 68 1 67 18.5079437… 19.01216… 165.330220146…
3 X3 time.txt:start… 2040 time.txt:s… 2940 900 155 0 155 22.1224503… 22.60069… 546.209379862…
4 Path Code1 Code1 Code1 Code1 Code1 Code1 Code1 Code1 Code1 Code1 Code1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496599.html