基于另一个数据帧改变数据帧的值 [英] Changing value of data frame based on another data frame

查看:123
本文介绍了基于另一个数据帧改变数据帧的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次,我需要你的帮助一个可能容易的问题,对于初学者R用户来说不清楚。



我需要操作一个数据框,以逼真的方式替换 NA 值来提供另一个应用程序。 p>

数据帧包含-3.0的值,它是原始数据库中非有效值的标志。我需要的是通过来自另一个数据帧的数据来替换所有-3.0值,或者可以内插。



第一个数据帧将是

  1.0 2.0 3.0 4.0 
2.0 3.0 -3.0 -3.0
1.0 4.0 -3.0 6.0
1.0 5.0 4.0 5.0

第二个将是

  1.0 1.0 1.0 1.0 
2.0 2.0 9.0 9.0
2.0 2.0 9.0 2.0
1.0 1.0 1.0 1.0

和预期结果

  1.0 2.0 3.0 4.0 
2.0 3.0 9.0 9.0
1.0 4.0 9.0 6.0
1.0 5.0 4.0 5.0

我想这可以用一个for循环,但是我没有找到办法。



提前感谢

解决方案

在没有for循环的情况下,这样做很简单:如果你的数据框是 A B ,那么命令将是

  A [A == -3] = B [A == -3] 

换句话说:对于具有值-3的 A 的所有索引,分配$ code> B 在相应的索引。


Again I need your help for a maybe easy question that is not clear for a starter R user.

I need to manipulate a dataframe to substitute NA values by "realistic" ones to feed another application.

The data frame contains values of -3.0 that was the flag for non valid values in the original data base. What I need is to replace all the -3.0 values by data coming from another data frame, or maybe to interpolate.

The first data frame would be

1.0  2.0  3.0  4.0
2.0  3.0 -3.0 -3.0
1.0  4.0 -3.0  6.0
1.0  5.0  4.0  5.0

the second one would be

1.0  1.0  1.0  1.0
2.0  2.0  9.0  9.0
2.0  2.0  9.0  2.0
1.0  1.0  1.0  1.0

and the expected result

1.0  2.0  3.0  4.0
2.0  3.0  9.0  9.0
1.0  4.0  9.0  6.0
1.0  5.0  4.0  5.0

I suppose this can be done with a for loop but I haven't found the way to do it.

Thanks in advance

解决方案

It's actually quite simple to do this without a for loop: if your data frames are A and B, then the command would be

A[A == -3] = B[A == -3]

In other words: for all the indices of A that have value -3, assign the values of B at the corresponding indices.

这篇关于基于另一个数据帧改变数据帧的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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