【问题标题】:Change one specific value in a data table in R [duplicate]在R中更改数据表中的一个特定值[重复]
【发布时间】:2018-10-04 03:14:18
【问题描述】:

这是我的代码

nutrients<- read.csv("nutrients.csv", head = TRUE, sep = ",")
> plot(nutrients)
> head(nutrients)
         crop Nutrient.dens N..tons.acre. P2O5 K2O sum.nut
1    broccoli         340.0           210  245 100     555
2      carrot         458.0            70  250  50     370
3 cauliflower         315.0            25   35  80     140
4     letuce          318.5           165  150  90     405
5       onion         109.0           120   30 150     300
6      tomato         186.0           175   85 275     535
> df_nutrients<- as.data.frame(nutrients)
> df_nutrients<- df_nutrients[1,1=="broc"]

我相信这很容易,我尝试搜索任何我能找到的东西来获得答案,但我找不到它。我只需要将一个变量更改为“broc”。我需要什么特定的功能吗?

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    如果crop 是一个字符类型,那么一个简单的子集应该可以工作

    nutrients$crop[nutrients$crop == "broccoli"] <- "broc"
    

    如果crop 是一个因素,那么使用这个:

    levels(nutrients$crop)[levels(nutrients$crop) == "broccoli"] <- "proc"
    

    【讨论】:

    • 成功了,谢谢!
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多