使用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生成 ℹ输入SpeciesSpecies。 2:在replace(Species,"new variable")[<-.factor的列表中,值=“新变量”): 无效的因子水平,NA生成

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...