用数据框中其他列的值替换空值

编程入门 行业动态 更新时间:2024-10-24 11:25:02
本文介绍了用数据框中其他列的值替换空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的数据框的一列中,我有一些空单元格。数据如下所示:

In one column of my dataframe I have some empty cells. The data looks like this:

yymmdd lat lon mag depth knmilocatie baglocatie tijd 19861226 52.992 6.548 2.8 1.0 Assen Assen 74751 19871214 52.928 6.552 2.5 1.5 Hooghalen 204951 19891201 52.529 4.971 2.7 1.2 Purmerend Kwadijk 200914 19910215 52.771 6.914 2.2 3.0 Emmen Emmen 21116 19910425 52.952 6.575 2.6 3.0 Geelbroek Ekehaar 102631 19910808 52.965 6.573 2.7 3.0 Eleveld Assen 40114

所需的结果是:

yymmdd lat lon mag depth knmilocatie baglocatie tijd 19861226 52.992 6.548 2.8 1.0 Assen Assen 74751 19871214 52.928 6.552 2.5 1.5 Hooghalen Hooghalen 204951 19891201 52.529 4.971 2.7 1.2 Purmerend Kwadijk 200914 19910215 52.771 6.914 2.2 3.0 Emmen Emmen 21116 19910425 52.952 6.575 2.6 3.0 Geelbroek Ekehaar 102631 19910808 52.965 6.573 2.7 3.0 Eleveld Assen 40114

灵感来自这个解决方案,我试图用以下方式替换空单元格:

Inspired by this solution, I tried to replace the empty cells with:

df$baglocatie[df$baglocatie == ""] <- df$knmilocatie

然而,这没有工作,因为空单元格填满了错误的值。 另一种可能的解决方案也无效。

However, this did not work as the empty cells were filled with the wrong values. Another possible solution also didn't work.

如何我解决了这个问题?

How do I solve this?

推荐答案

尝试 ifelse :

df$baglocatie <- ifelse(df$baglocatie == "", df$knmilocatie, df$baglocatie)

更多推荐

用数据框中其他列的值替换空值

本文发布于:2023-10-14 12:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!