R - 使用任何其他列的值填充列

时间:2017-02-12 08:07:08

标签: r dataframe multiple-columns

我有一个5列的数据框:4列有值,1列是空的。我想用4列中的任何一列填充空列。

我们假设这是我的数据框df

Col1 Col2 Col3 Col4 Col5
  11   11    
   2         2    2
       23
   4         4
       15        15

我希望我的结果看起来像这样:

Col1 Col2 Col3 Col4 Col5
  11   11             11
   2         2    2    2
       23             23
   4         4         4
       15        15   15

编辑我应用了每个人提供的答案,但由于某种原因它仍然无法正常工作。如果有帮助,这是我实际数据的dput(head(df))

structure(list(Treat_One = c("      ", "5 2012", "4 2008", "4 2010", 
"      ", "2 2008"), Treat_Two = c("8 2010", "5 2012", "4 2008", 
"4 2010", "8 2011", "2 2008"), Treat_Three = c("      ", "5 2012", 
"4 2008", "4 2010", "8 2011", "2 2008"), Treat_Four = c("      ", 
"      ", "      ", "      ", "      ", "      ")), .Names = c("Treat_One", 
"Treat_Two", "Treat_Three", "Treat_Four"), row.names = c(NA, 
6L), class = "data.frame")

修改包含str(df)

'data.frame':   209 obs. of  4 variables:
 $ Treat_One  : chr  "      " "5 2012" "4 2008" "4 2010" ...
 $ Treat_Two  : chr  "8 2010" "5 2012" "4 2008" "4 2010" ...
 $ Treat_Three: chr  "      " "5 2012" "4 2008" "4 2010" ...
 $ Treat_Four : chr  "      " "      " "      " "      " ...

4 个答案:

答案 0 :(得分:0)

您只需键入以下内容:
    df$Col5 <- 1:5
df$Col5会在Col5df中创建1:5,只需在其中添加序列号。

答案 1 :(得分:0)

根据OP提供的新数据,我们可以使用trimws删除前导/尾随空格

df$Treat_Four <- apply(df, 1, function(x) sample(x[trimws(x) != ""], 1))
df

#    Treat_One Treat_Two Treat_Three Treat_Four
#1              8 2010                 8 2010
#2    5 2012    5 2012      5 2012     5 2012
#3    4 2008    4 2008      4 2008     4 2008
#4    4 2010    4 2010      4 2010     4 2010
#5              8 2011      8 2011     8 2011
#6    2 2008    2 2008      2 2008     2 2008

原始答案

我们可以逐行使用apply并获取不等于空字符串的元素的1 sample

df$Col5 <- apply(df, 1, function(x) sample(x[x != ""], 1))
df
#  Col1 Col2 Col3 Col4 Col5
#1    1    1              1
#2    2         2    2    2
#3         3              3
#4    4         4         4
#5         5         5    5

如果有NA个值而不是空格,我们可以使用相同的逻辑

apply(df, 1, function(x) sample(x[!is.na(x)], 1))

答案 2 :(得分:0)

试试这个:

df <- data.frame(col1 = c(1, NA, 3), col2 = c(1, 2, NA), col3 = c(NA, 2, 3),col4 = rep(NA, 3))
for (i in 1:nrow(df)) {
    df[i, 4] <- df[i, which(!is.na(df[i,]))][, 1]
}
df

这会产生:

> df <- data.frame(col1 = c(1, NA, 3), col2 = c(1, 2, NA), col3 = c(NA, 2, 3), col4 = rep(NA, 3))
> df
col1 col2 col3 col4
1    1    1   NA   NA
2   NA    2    2   NA
3    3   NA    3   NA
> for (i in 1:nrow(df)) {
+     df[i, 4] <- df[i, which(!is.na(df[i,]))][, 1]
+ }
+ df
+ 
col1 col2 col3 col4
1    1    1   NA    1
2   NA    2    2    2
3    3   NA    3    3

答案 3 :(得分:-1)

以下是max.col

的矢量化选项
df$Treat_Four <- df[1:3][cbind(1:nrow(df), max.col(sapply(df[1:3], trimws)!='', "first"))]
df
#   Treat_One Treat_Two Treat_Three Treat_Four
#1              8 2010                 8 2010
#2    5 2012    5 2012      5 2012     5 2012
#3    4 2008    4 2008      4 2008     4 2008
#4    4 2010    4 2010      4 2010     4 2010
#5              8 2011      8 2011     8 2011
#6    2 2008    2 2008      2 2008     2 2008