I want to change values below 60 in a column of my dataframe but values below 10 are not taken with it

huangapple go评论66阅读模式
英文:

I want to change values below 60 in a column of my dataframe but values below 10 are not taken with it

问题

在名为 "CKD" 的列中,您想将所有小于60的值更改为 "Yes",大于等于60的值更改为 "No"。

但是,当您运行以下代码时:

Student_data_2$CKD[Student_data_2$CKD >= 60] <- "No"
Student_data_2$CKD[Student_data_2$CKD < 60] <- "Yes"

除了小于10的值之外,所有值都被更改为文本。这可能是因为小于10的值被视为字符而不是数字。要解决此问题,您可以将小于10的条件更改为字符串,并重新运行代码。这是修正后的代码:

Student_data_2$CKD[Student_data_2$CKD >= 60] <- "No"
Student_data_2$CKD[Student_data_2$CKD < "10"] <- "Yes"

现在,小于10的值应该正确地被更改为 "Yes"。

英文:

I have a dataset and i want to change values in my column to "No" and "Yes"

This is the dataset:

gender      age     creat     eGFR      CKD
1      1 20.75420  70.35006 66.77359 66.77359
2      1 40.48984 102.39087 37.64458 37.64458
4      1 38.91398  92.26064 43.07771 43.07771
5      1 27.57514  71.13583 63.15280 63.15280
6      0 51.90974 218.35305 10.57759 10.57759
7      1 25.56240  86.98399 50.23518 50.23518

In the column called "CKD" i want to change all the values below 60 to "Yes" and greater than or equal to CKD to "No".

However, when i run the following code:

Student_data_2$CKD[Student_data_2$CKD &gt;= 60] &lt;- &quot;No&quot; 
Student_data_2$CKD[Student_data_2$CKD &lt; 60] &lt;- &quot;Yes&quot; 

All values are changed except for the values below 10, they stay numeric. For example:

  gender      age     creat       eGFR              CKD
1        1 20.75420  70.35006  66.773594               No
2        1 40.48984 102.39087  37.644583              Yes
4        1 38.91398  92.26064  43.077715              Yes
5        1 27.57514  71.13583  63.152798               No
15       0 42.27583 196.62734  12.735807              Yes
16       0 59.43180 185.07882  12.309281              Yes
17       1 21.16721 102.46636  42.413975              Yes
18       0 51.78945 131.65945  19.424971              Yes
19       0 61.89885 253.39297   8.314646 8.31464627872699
20       0 76.81865 215.07378   9.225582 9.22558204994784
22       0 53.63316 236.28427   9.519215 9.51921545447276
23       1 31.24610  93.35322  44.548002              Yes

Does anyone know what is the problem with my code?

答案1

得分: 4

当您运行第一行代码时,所有数字都会被转换为字符,因此您的第二行不会按预期运行。您需要创建一个新列或一次性完成整个比较。尝试这样做:

Student_data_2$CKD <- ifelse(Student_data_2$CKD >= 60, "No", "Yes")
英文:

As soon as you run your first line of code, all the numbers become converted to characters, so your second line isn't behaving as expected. You need to create a new column or do the entire comparison at once. Try this:

Student_data_2$CKD  &lt;- ifelse(Student_data_2$CKD &gt;= 60, &quot;No&quot;, &quot;Yes&quot;)

huangapple
  • 本文由 发表于 2023年6月22日 04:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定