问题描述
我想将列中的特定值更改为另一个值。我想使用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 (将#修改为@)