Test for NA and select values based on result

こ雲淡風輕ζ 提交于 2019-12-18 03:03:31

问题


My question is rather simple. What I want is if A[i]!=NA, then C[i]=A[i], if A[i]=NA, then C[i]=B[i], however, I always get some error messages. Can somebody help me out?

A   B   C
NA  82.6    .
NA  127.2   .
NA  93.6    .
NA  105 .
NA  104 .
NA  90.6    .
NA  95.8    .
NA  103 .
NA  85.4    .
NA  81.5    .
NA  142.8   .
NA  102.3   .
NA  104 .
NA  103 .
NA  94.6    .
NA  113.8   .
NA  113.5   .
NA  74.5    .
NA  123.8   .
NA  94  .
NA  89.8    .
NA  74  .
NA  104 .
NA  100.5   .
NA  102.9   .
NA  132.5   .
NA  91  .
NA  92.5    .
NA  97  .
NA  90  .
54.6    51.7    .
NA  61  .
NA  80  .
NA  77.5    .
NA  NA  .
NA  80.6    .
NA  44.6    .
NA  37.6    .
NA  27  .
NA  NA  .
NA  NA  .
NA  NA  .

回答1:


use is.na :

DF <- within(DF,
   C <- ifelse(!is.na(A),A,B)
)

with DF being your dataframe.



来源:https://stackoverflow.com/questions/7488068/test-for-na-and-select-values-based-on-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!