簡體   English   中英

如何在數據表中添加一列,以顯示其他多個列的值之和?

[英]How do I add a column to my data table that shows the sum of multiple other columns' values?

我有8個年齡類別,每個類別都有其自己的列(即,residents_under_5,residents_6_to_12等)。對於該特定年齡類別中該家庭的人數,每列的值都介於0到3之間。一個新的列,用它可以在直方圖上繪制我的人口年齡的總分布,因此我想到的一列包含這些類別的總和的66行residents_under_5、32行residents_6_to_12等。

我的數據如下所示:

a b c d 
0 3 2 1
1 3 2 1
2 0 2 1
3 1 0 0

我想要的是顯示以下內容的列e:

e
a
a
a
a
b
b
b
b
b
c
c
c
d
d
d

對於其他列中的發生總數。

我嘗試用sum(residents_under_5)聲明新列,但這將給我1行66(作為該類別的總和)。 我無法用這樣的列繪制直方圖。 我希望有人能弄清楚!

這是相關列的dput()

residents_under_5 = c(0, 0, 0, 1, 1, 2), 
residents_6_to_12 = c(0, 0, 0, 0, 0, 0), 
        residents_13_to_18 = c(0, 0, 0, 0, 0, 0), 
residents_19_to_24 = c(0, 
        0, 0, 0, 0, 0), 
residents_25_to_34 = c(0, 1, 2, 0, 1, 0), 
       residents_35_to_49 = c(0, 0, 0, 2, 1, 2), 
residents_50_to_64 = c(0, 
        1, 0, 0, 0, 0), 
residents_65_and_older = c(2, 0, 0, 0, 1, 
        0)

您可以unlist數據幀並使用table計算頻率,然后使用rep重復letters

rep(letters[seq_len(ncol(df))], colSums(df))

數據

df <- data.frame(residents_under_5 = c(0, 0, 0, 1, 1, 2), 
                 residents_6_to_12 = c(0, 0, 0, 0, 0, 0), 
                 residents_13_to_18 = c(0, 0, 0, 0, 0, 0), 
                 residents_19_to_24 = c(0, 0, 0, 0, 0, 0), 
                 residents_25_to_34 = c(0, 1, 2, 0, 1, 0), 
                 residents_35_to_49 = c(0, 0, 0, 2, 1, 2), 
                 residents_50_to_64 = c(0, 1, 0, 0, 0, 0), 
                 residents_65_and_older = c(2, 0, 0, 0, 1, 0))

在選項tidyverse將得到sum與所有列的summarise_allgather成“長”格式和uncount的“價值”列

library(tidyverse)
df1 %>% 
   summarise_all(sum) %>%
   gather %>% 
   uncount(value)

數據

df1 <- structure(list(a = 0:3, b = c(3L, 3L, 0L, 1L), c = c(2L, 2L, 
2L, 0L), d = c(1L, 1L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
  -4L))

暫無
暫無

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

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