如何使用R中的if-else替换DF中的值? [英] How to substitute several NA with values within the DF using if-else in R?

查看:180
本文介绍了如何使用R中的if-else替换DF中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢你的时间。我有以下数据(摘录)。它来自纵向数据,改为宽格式的工作状态文件,每个列代表一个月,每行代表一个人。

 代码:
j1992_12 = c(1,10,1,7,1,1)
j1993_01 = c(1,1,1,NA,3,1)
j1993_02 = c(1,1,1,NA,3,1)
j1993_03 = c(1,8,1,NA,3,1)
j1993_04 = c(1,8,1,NA, 3,1)
j1993_05 = c(1,8,1,NA,3,1)
j1993_06 = c(1,8,1,NA,3,1)
j1993_07 = c(1,8,1,NA,3,1)
j1993_08 = c(1,8,1,NA,3,1)
j1993_09 = c(1,8,1,NA, 3,1)
j1993_10 = c(1,8,1,NA,3,1)
j1993_11 = c(1,8,1,NA,3,1)
j1993_12 = c(1,8,1,NA,3,1)
j1994_01 = c(1,8,1,7,3,1)


DF93 =数据。帧(j1992_12,j1993_01,j1993_02,j1993_03,j1993_04,j1993_05,j1993_06,j1993_07,j1993_08,j1993_09,j1993_10,j1993_11,j1993_12,j1994_01)


输出:
j1992_12 j1993_01 j1993_02 j1993_03 j1993_04 j1993_05 j1993_06 j1993_07 j1993_0 8 j1993_09 j1993_10 j1993_11 j1993_12 j1994_01
R1 1 1 1 1 1 1 1 1 1 1 1 1 1
R2 10 1 1 8 8 8 8 8 8 8 8 8 8
R3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
R4 7 NA NA NA NA NA NA NA NA NA NA NA 7
R5 1 3 3 3 3 3 3 3 3 3 3 3 3 3
R6 1 1 1 1 1 1 1 1 1 1 1 1 1

我的希望检查12个月的发生时间与NA一样e R4。我想检查一年之前的最后一次出现(j1992_12)是否与下一年的第一次出现((j1994_01)具有相同的值。如果是,我假设工作状态没有变化,因此所有12几个月应该得到一年前最后一个月给出的价值。如果没有,那么所有的都应该保持不变。



到目前为止的方法:

  DF93_2 = DF93 
DF93_2 [,2:13]< - ifelse(is.na(DF93 [,2:13]) &&(DF93 [,1] == DF93 [,14]),DF93 [,1],DF93 [,2:13])

我现在看到,如果我只用一个像下面的代码那样的字体来尝试它,它就会取代整个列。如何教R只是替换rowwise?

  DF93_2 [,2]<  -  ifelse(is.na(DF93 [,2:13])& ;&(DF93 [,1] == DF93 [,14]),DF93 [,1],DF93 [,2])

如果有人能给我一个暗示我对R的理解存在缺陷的提示,我将非常感激。



编辑!只有原始文件是纵向的,此格式现在是WIDE以及我需要的时间序列分析。它已经与所有年份(18年,从1992年开始到2010年)的调查数据进行了交叉检查,所以我宁愿不转换成长格式,我正在寻找一种可能的条件,如上所述,我可以调整条件不同。



经过进一步测试,我认为问题在于连续搜索12个后续NA。我只是找不到解决方案。如果您有任何想法,请分享。谢谢!

解决方案

以下是一种方式:

  as.data.frame(t(apply(DF93,1,function(x)
if(x [1] == tail(x,1)&& all(is。 na(head(x,-1)[ - 1])))
replace(x,is.na(x),x [1])else x)))


thank you for your time. I have the following data (snippet). Its from longitudinal data, reformed to a wide-format-file of work status, each colum represents one month, each row an individual.

Code:
j1992_12 = c(1, 10, 1, 7, 1, 1)
j1993_01 = c( 1, 1, 1, NA, 3, 1) 
j1993_02 = c( 1, 1, 1, NA, 3, 1) 
j1993_03 = c( 1, 8, 1, NA, 3, 1) 
j1993_04 = c( 1, 8, 1, NA, 3, 1) 
j1993_05 = c( 1, 8, 1, NA, 3, 1) 
j1993_06 = c( 1, 8, 1, NA, 3, 1) 
j1993_07 = c( 1, 8, 1, NA, 3, 1) 
j1993_08 = c( 1, 8, 1, NA, 3, 1) 
j1993_09 = c( 1, 8, 1, NA, 3, 1) 
j1993_10 = c( 1, 8, 1, NA, 3, 1) 
j1993_11 = c( 1, 8, 1, NA, 3, 1) 
j1993_12 = c( 1, 8, 1, NA, 3, 1) 
j1994_01 = c( 1, 8, 1, 7, 3, 1) 


DF93= data.frame(j1992_12, j1993_01, j1993_02, j1993_03, j1993_04, j1993_05, j1993_06, j1993_07, j1993_08, j1993_09, j1993_10, j1993_11, j1993_12, j1994_01)


Output:
       j1992_12   j1993_01 j1993_02 j1993_03 j1993_04 j1993_05 j1993_06 j1993_07 j1993_08 j1993_09 j1993_10 j1993_11 j1993_12 j1994_01
    R1        1          1        1        1        1        1        1        1        1        1        1        1        1        1
    R2       10          1        1        8        8        8        8        8        8        8        8        8        8        8
    R3        1          1        1        1        1        1        1        1        1        1        1        1        1        1
    R4        7         NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA        7
    R5        1          3        3        3        3        3        3        3        3        3        3        3        3        3
    R6        1          1        1        1        1        1        1        1        1        1        1        1        1        1

My wish is to check für occurrences of 12 months straight withe "NA" as in line R4. I would like then to check if the last occurence of the year before (j1992_12) has the same value as the first occurence of the year that follows ((j1994_01). If yes I assume there was no change in work status and therefore all 12 months should get the value, that is given in the last month of the year before. If not, all should stay untouched.

Method so far:

DF93_2 = DF93
DF93_2[,2:13] <- ifelse (is.na( DF93[,2:13]) && (DF93[,1]==DF93[,14]), DF93[,1] , DF93[,2:13])

I now see, that if I try it with just a single colum like the code beneath, it replaces the whole column. How to teach R to just replace rowwise?

DF93_2[,2] <- ifelse (is.na( DF93[,2:13]) && (DF93[,1]==DF93[,14]), DF93[,1] , DF93[,2])

If someone could please give me a hint where the flaw in my understanding of R is, I would be very grateful.

EDIT! Only the original file is longitudinal, this format now is WIDE and what I need for a time series analysis. It is already cross-checked with survey data of all years (18 years, beginning 1992 going to 2010) so I would rather not retransform in into long-format an am looking for an possibility with conditions as pointed out above, that I could adjust as the condition differs.

After further testing, I think the problem lies within the search for 12 subsequent NA in a row. I just cannot find a solution to that. If you have any idea, please share. Thank you!

解决方案

Here's one way:

as.data.frame(t(apply(DF93, 1, function(x) 
  if(x[1] == tail(x, 1) && all(is.na(head(x, -1)[-1]))) 
    replace(x, is.na(x), x[1]) else x)))

这篇关于如何使用R中的if-else替换DF中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆