有条件地填充行

时间:2014-05-21 03:50:35

标签: r

我确信这很简单,但它(并且感冒了)此刻让我感到难过。我有以下列表,C1& C2。我想删除x,用通过C1编号关联的另一个字母替换它们,给出Desired C2列结果。非x值始终位于第一个位置。

   C1 C2 DesiredC2
1   1  A         A
2   1  A         A
3   1  x         A
4   1  A         A
5   1  x         A
6   2  B         B
7   2  B         B
8   2  x         B
9   3  C         C
10  3  x         C

前两列的dput()版本是:

df2 <- structure(list(C1 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), 
C2 = c("A", "A", "x", "A", "x", "B", "B", "x", "C", "x"), 
X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("C1","C2", "X"), class = "data.frame", row.names = c(NA, -10L))

谢谢。

4 个答案:

答案 0 :(得分:2)

这将产生所需的结果

df2$X <- with(df2, ifelse(C2 != "x", C2, unique(C2[C2 != "x"])[C1]))

或者,如果要替换的是C2中的“QC”条目:

df2$X <- with(df2, ifelse(C2 != "QC", C2, unique(C2[C2 != "QC"])[C1]))

OP在其他答案中评论后编辑:

df2$X <- with(df2, ifelse(C2 != "x", C2, LETTERS[C1]))

这将始终根据C1给出相应的字母,其中C2中有“x”。

在OP的另一条评论之后编辑#2

df2$X <- with(df2, ifelse(is.na(C2), LETTERS[C1], C2))    #for NA instead of "x"

答案 1 :(得分:1)

尝试:

#Find the "x"s
xs = which(df2$C2=="x") 
#Now replace them
df2$C2[xs] = LETTERS[df2$C1[xs]]

那是你所追求的吗?

答案 2 :(得分:1)

ave再次让人头疼:

with(df2, ave(C2,C1,FUN=function(i) i[i!="x"][1])  )
#[1] "A" "A" "A" "A" "A" "B" "B" "B" "C" "C"

...并考虑NA值:

with(df2, ave(C2,C1,FUN=function(i) i[i!="x" & !is.na(i)][1])  )
#[1] "A" "A" "A" "A" "A" "B" "B" "B" "C" "C"

要解释其工作原理,aveC2拆分为C1定义的组,然后返回第一个非&#34; x&#34;组中每个条目的C2值。即 -

i[i!="x"][1]

转换为

C2[C2 != "x"][1]

答案 3 :(得分:0)

我希望以下内容对您有用...我试图让它成为通用的......您可以跳过几步......

修改

df2_x = df2[df2$C2=='x',] #Take out rows with X
df2_nx = df2[df2$C2!='x',] #Take out rows without X


#Dummy Dataframe - Which is one to one map. Here C2 can have any value, character/number/string
structure(list(C1 = c(2L, 3L, 1L), C2 = structure(c(2L, 1L, 3L
), .Label = c("A", "B", "C"), class = "factor")), .Names = c("C1", 
"C2"), row.names = c(NA, -3L), class = "data.frame")

  C1 C2
1  2  B
2  3  A
3  1  C

#Find out remaining colnames (except C2)
rem_colNames = setdiff(colnames(df2),'C2')
df2_x = merge(df2_x[,rem_colNames],mappingdf,by='C1')

#Combining both data.frame
df2 = rbind(df2_nx,df2_x)