简体   繁体   English

如何将 1 添加到包含整数和 NA(在 R 中)的数据集中的所有整数?

[英]How can I add 1 to all integers in a dataset consiting integers and NAs (in R)?

I have a dataset consisting of integers and NAs.我有一个由整数和 NA 组成的数据集。 I want to add 1 (+1) to all of the integers but not to the NAs.我想将 1 (+1) 添加到所有整数,但不添加到 NA。 How can I do this?我怎样才能做到这一点? Thanks for your help!谢谢你的帮助!

Assuming your df is named df and var1 is the column you want to add 1 to:假设您的 df 名为df并且var1是您要添加 1 的列:

df$var2 = df$var1 +1

where var2 will be the column having the final result.其中var2将是具有最终结果的列。

PS Adding 1 to NA still returns NA. PS 向 NA 添加 1 仍然返回 NA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM