如何解决使用dplyr和pipe函数有条件地更改列的值
我想将列中的特定值更改为另一个值。我想使用dplyr和pipe函数,并且我不想创建向量然后替换这些值。例如,在虹膜数据集的“种类”列中,我想将等于“ setosa”的值更改为一个名为“ new value”的新值。 recode()不适用于data.frmaes。
unit = input("Select (K)kilograms or (L)pounds: ").upper()
if unit == "L":
weight = int(float(input("give your weight: ")))
converted_weight = weight * 0.45
print(f"your weight {converted_weight} converted to kilograms")
elif unit == "K":
weight = int(float(input("give your weight: ")))
converted_weight = weight / 0.45
print(f"your weight {converted_weight} converted to pounds")
else:
raise ValueError("please choose between k or L")
我只想为一个值指定一个更改,其余的保持不变。
使用mutate()和replace()也不起作用:
library(tidyverse)
temp <- iris %>%
recode(Species,"setosa" = "new value")
我明白了:
警告消息:
1: mutate(Species = replace(Species,Species == "setosa","new variable"))
输入mutate()
有问题。
factor无效的因子水平,NA生成
ℹ输入Species
为Species
。
2:在replace(Species,"new variable")
([<-.factor
的列表中,值=“新变量”):
无效的因子水平,NA生成
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。