根据前一行

时间:2015-07-31 15:29:12

标签: r fill

我有一张巨大的桌子,基本上是这样的:

A  B  C  D  E  F
A  B        &
A  B  C  D     $
A  B  C  @

处理后的版本应如下所示:

A  B  C  D  E  F
A  B  B& B& B& B&
A  B  C  D  D$ D$
A  B  C  C@ C@ C@

任务是将最后一个非空单元格的值与前一个非空单元格(在同一行中)的值连接起来,并使用新值填充同一行中的空单元格。

有关如何在 R 中执行此操作的建议吗?

3 个答案:

答案 0 :(得分:3)

这是循环数据集行的一个选项。我们通过选择非空白元素('x1'),paste'x1'中最后两个非空白元素('x2')来对每行的元素进行子集化,然后连接所有值除了最后一个(head(x1,-1)),其中'x2'值是根据'df1'的列数和'x1'的length复制的。结果可以转置(t)并转换为data.frame

 m1 <- t(apply(df1, 1, function(x) {
          x1 <- x[x!=''] #elements that are not-blank
          x2 <- paste(tail(x1,2), collapse='') #paste  the last two non-blank
          if(any(x=='')) #if there is any blank value
          c(head(x1,-1), rep(x2, ncol(df1)-length(x1)+1)) #concatenate
          else x #else return the row
           }))

 as.data.frame(m1, stringsAsFactors=FALSE)
 #  V1 V2 V3 V4 V5 V6
 #1  A  B  C  D  E F
 #2  A  B B& B& B& B&
 #3  A  B  C  D D$ D$
 #4  A  B  C C@ C@ C@

数据

 df1 <- structure(list(v1 = c("A", "A", "A", "A"), v2 = c("B", "B", "B", 
 "B"), v3 = c("C", "", "C", "C"), v4 = c("D", "", "D", "@"), v5 = c("E", 
 "&", "", ""), v6 = c("F", "", "$", "")), .Names = c("v1", "v2", 
 "v3", "v4", "v5", "v6"), class = "data.frame", row.names = c(NA, -4L))

答案 1 :(得分:1)

此问题从zoo

尖叫""

首先,将NA替换为x[sapply(x,function(y)y=="X")]<-NAx.no.sym<-x x.no.sym[sapply(x.no.sym,function(y)!y%in%LETTERS)]<-NA

剥离符号:

x.no.sym.fill<-t(apply(x.no.sym,1,na.locf))
     V1  V2  V3  V4  V5  V6 
[1,] "A" "B" "C" "D" "E" "F"
[2,] "A" "B" "B" "B" "B" "B"
[3,] "A" "B" "C" "D" "D" "D"
[4,] "A" "B" "C" "C" "C" "C"

填写信件:

x.sym.fill<-x.sym.fill<-t(apply(x,1,function(y)na.locf(na.locf(y,fromLast=T,na.rm=F),na.rm=F)))
x.sym.fill[sapply(x.sym.fill,function(y)y%in%LETTERS)]<-""
     V1 V2 V3  V4  V5  V6 
[1,] "" "" ""  ""  ""  "" 
[2,] "" "" "&" "&" "&" "&"
[3,] "" "" ""  ""  "$" "$"
[4,] "" "" ""  "@" "@" "@"

现在填写符号并删除字母:

> matrix(paste0(x.no.sym.fill,x.sym.fill),ncol=ncol(x))

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "A"  "B"  "C"  "D"  "E"  "F" 
[2,] "A"  "B"  "B&" "B&" "B&" "B&"
[3,] "A"  "B"  "C"  "D"  "D$" "D$"
[4,] "A"  "B"  "C"  "C@" "C@" "C@"

现在连接:

EM::stop_server

答案 2 :(得分:0)

这个看起来很有趣。我将数据框中的空格设为""并调用数据框df

fill = apply(df, 1, function(x) { 
  x = x[x != ""]
  paste(tail(x, 2), collapse = "")
})

df[df == ""] = matrix(fill, ncol = ncol(df), nrow = nrow(df))[df == ""]

为每一行找到唯一的填充值,制作与原始填充值相同结构的矩阵,然后选择需要替换的值。

df = structure(list(A = c("A", "A", "A"), B = c("B", "B", "B"), C = c("", 
"C", "C"), D = c("", "D", "@"), E = c("&", "", ""), F = c("", 
"$", "")), .Names = c("A", "B", "C", "D", "E", "F"), row.names = c(NA, 
-3L), class = "data.frame")