簡體   English   中英

如何在 R 中將我的數據幀從寬轉換為長?

[英]How can I transform my data frame from wide to long in R?

我在將數據框從寬轉換為長時遇到問題。 我很清楚那里有很多優秀的小插曲,它們非常精確地解釋了 gather() 或 pivot_longer() (例如https://www.storybench.org/pivoting-data-from-columns-to-rows-和-回到-tidyverse/ )。 盡管如此,我現在仍然被困了好幾天,這讓我發瘋。 於是,我特意去網上問問。 你。

我有一個看起來像這樣的數據框:

id     <- c(1,2,3)
year   <- c(2018,2003,2011)
lvl    <- c("A","B","C")
item.1 <- factor(c("A","A","C"),levels = lvl)
item.2 <- factor(c("C","B","A"),levels = lvl)
item.3 <- factor(c("B","B","C"),levels = lvl)
df     <- data.frame(id,year,item.1,item.2,item.3)

所以我們為每個觀察(例如電影)都有一個 id 變量。 我們有一個年份變量,表示觀察發生的時間(例如電影上映的時間)。 我們有三個因素變量來評估觀察的不同特征(例如演員、故事情節和電影音樂)。 這三個因子變量共享相同的因子水平“A”、“B”或“C”(例如,電影的演員陣容是“優秀”、“還可以”或“糟糕”)。

但在我最瘋狂的夢想中,數據更像是這樣的:

id.II     <- c(rep(1, 9), rep(2, 9), rep(3,9))
year.II   <- c(rep(2018, 9), rep(2003, 9), rep(2011,9))
item.II   <- rep(c(c(1,1,1),c(2,2,2),c(3,3,3)),3)
rating.II <- rep(c("A", "B", "C"), 9)
number.II  <- c(1,0,0,0,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,0,1,1,0,0,0,0,1)
df.II     <- data.frame(id.II,year.II,item.II,rating.II,number.II)

因此,現在數據框將更可用於進一步分析。 例如,下一步將計算每年被評為“優秀”的電影的數量(甚至更高的百分比)。

year.III   <- factor(c(rep(2018, 3), rep(2003, 3), rep(2011,3)))
item.III   <- factor(rep(c(1, 2, 3), 3))
number.A.III <- c(1,0,0,1,0,0,0,1,0)
df.III     <- data.frame(year.III,item.III,number.A.III)

ggplot(data=df.III, aes(x=year.III, y=number.A.III, group=item.III)) +
  geom_line(aes(color=item.III))+
  geom_point(aes(color=item.III))+
  theme(panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "bottom")+
  labs(colour="Item")

或者對我來說更重要的是,顯示每個項目(演員、講故事、電影音樂)被評為“優秀”、“還可以”和“糟糕”的百分比。

item.IV   <- factor(rep(c(c(1,1,1),c(2,2,2),c(3,3,3)),3))
rating.IV <- factor(rep(c("A", "B", "C"), 9))
number.IV <- c(2,0,1,1,1,1,0,2,1)
df.IV     <- data.frame(item.IV,rating.IV,number.IV)
df.IV

ggplot(df.IV,aes(fill=rating.IV,y=number.IV,x=item.IV))+
  geom_bar(position= position_fill(reverse = TRUE), stat="identity")+
  theme(axis.title.y = element_text(size = rel(1.2), angle = 0),
        axis.title.x = element_blank(),
        panel.background = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom")+
  labs(x = "Item")+
  coord_flip()+
  scale_x_discrete(limits = rev(levels(df.IV$item.IV)))+
  scale_y_continuous(labels = scales::percent)

我的主要問題是:如何將數據框 df 轉換為 df.II? 那會讓我很開心。 錯誤的。 我的周末。

如果您還可以提示如何從 df.II 繼續到 df.III 和 df.IV,那絕對是令人興奮的。 但是,我不想因為我的問題給你太多負擔。

最好的祝願 Jascha

這是否達到了您的需要?

library(tidyverse)

df_long <- df %>%
  pivot_longer(cols = item.1:item.3, names_to = "item", values_to = "rating") %>%
  mutate(
    item = str_remove(item, "item.")
  )


df2 <- crossing(
  df_long,
  rating_all = unique(df_long$rating)
) %>%
  mutate(n = rating_all == rating) %>%
  group_by(id, year, item, rating_all) %>%
  summarise(n = sum(n))

df3 <- df2 %>%
  filter(item == "3")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM