簡體   English   中英

如何整齊地組合稀疏列

[英]how to combine sparse columns tidily

一位同事有一些數據由許多稀疏列組成,這些列應該折疊成幾個填充列。 例如:

d1 <- data.frame(X1 = c(rep("Northampton", times=3), rep(NA, times=7)), 
                 X2 = c(rep(NA, times=3), rep("Amherst", times=5), rep(NA, times=2)), 
                 X3 = c(rep(NA, times=8), rep("Hadley", times=2)), 
                 X4 = c(rep("Stop and Shop", times=2), rep(NA, times=6), rep("Stop and Shop", times=2)), 
                 X5 = c(rep(NA, times=2), rep("Whole Foods", times=6), rep(NA, times=2)))

d1
            X1      X2     X3            X4          X5
1  Northampton    <NA>   <NA> Stop and Shop        <NA>
2  Northampton    <NA>   <NA> Stop and Shop        <NA>
3  Northampton    <NA>   <NA>          <NA> Whole Foods
4         <NA> Amherst   <NA>          <NA> Whole Foods
5         <NA> Amherst   <NA>          <NA> Whole Foods
6         <NA> Amherst   <NA>          <NA> Whole Foods
7         <NA> Amherst   <NA>          <NA> Whole Foods
8         <NA> Amherst   <NA>          <NA> Whole Foods
9         <NA>    <NA> Hadley Stop and Shop        <NA>
10        <NA>    <NA> Hadley Stop and Shop        <NA>

X1:X3應該折疊成一個名為Town的列,將X4:X5折疊到一個名為Store的列中。 這里必須有一個整齊的解決方案。 我嘗試過使用gather()unite()但是沒有找到任何優雅的東西。

你可以使用coalesce

d1 %>% mutate_if(is.factor, as.character) %>%    # coerce explicitly
    transmute(town = coalesce(X1, X2, X3), 
              store = coalesce(X4, X5))

##           town         store
## 1  Northampton Stop and Shop
## 2  Northampton Stop and Shop
## 3  Northampton   Whole Foods
## 4      Amherst   Whole Foods
## 5      Amherst   Whole Foods
## 6      Amherst   Whole Foods
## 7      Amherst   Whole Foods
## 8      Amherst   Whole Foods
## 9       Hadley Stop and Shop
## 10      Hadley Stop and Shop

我認為一系列的gather()調用和一些修剪會得到你想要的東西。 一個問題是使用na.rm = TRUE參數來gather()以剔除不需要的行。

d1 %>% 
  gather(key = "town", value = "town_name", X1:X3, na.rm = TRUE) %>% 
  gather(key = "store", value = "store_name", X4:X5, na.rm = TRUE) %>%
  select(-town, -store)

這樣做訣竅嗎?

您也可以在base R中執行此操作,並使用apply rowwise:

d2 <- data.frame(X1 = apply(d1[,c("X1", "X2", "X3")], 1, function(x) x[!is.na(x)]),
                 X2 = apply(d1[,c("X4", "X5")], 1, function(x) x[!is.na(x)]),
                 stringsAsFactors = FALSE)

結果:

> d2
            X1            X2
1  Northampton Stop and Shop
2  Northampton Stop and Shop
3  Northampton   Whole Foods
4      Amherst   Whole Foods
5      Amherst   Whole Foods
6      Amherst   Whole Foods
7      Amherst   Whole Foods
8      Amherst   Whole Foods
9       Hadley Stop and Shop
10      Hadley Stop and Shop

這是使用pmax/pmin base R另一種方式

data.frame(lapply(list(Town = d1[1:3], Store = d1[4:5]), function(x) 
           do.call(pmax, c(x, na.rm = TRUE))), stringsAsFactors=FALSE)
#          Town         Store
#1  Northampton Stop and Shop
#2  Northampton Stop and Shop
#3  Northampton   Whole Foods
#4      Amherst   Whole Foods
#5      Amherst   Whole Foods
#6      Amherst   Whole Foods
#7      Amherst   Whole Foods
#8      Amherst   Whole Foods
#9       Hadley Stop and Shop
#10      Hadley Stop and Shop

數據

d1 <- data.frame(X1 = c(rep("Northampton", times=3),rep(NA, times=7)),
   X2 = c(rep(NA, times=3), rep("Amherst", times=5), rep(NA, times=2)),
  X3 = c(rep(NA, times=8), rep("Hadley", times=2)), 
  X4 = c(rep("Stop and Shop", times=2), rep(NA, times=6), rep("Stop and Shop", times=2)), 
  X5 = c(rep(NA, times=2), rep("Whole Foods", times=6), 
        rep(NA, times=2)), stringsAsFactors=FALSE)

暫無
暫無

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

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