当使用R满足某些条件时,使用两列创建一个包含多个类别的新列(create a new column with multiple categories using two columns when th

编程入门 行业动态 更新时间:2024-10-26 02:37:29
当使用R满足某些条件时,使用两列创建一个包含多个类别的新列(create a new column with multiple categories using two columns when they satisfy certain conditions using R)

我有一个数据集“X”(值从0到80 )和“Y”(值从0到80 )。 我想创建一个新列“表”。 我有36张桌子:6人一组......他们应按照以下方式分组:

表1-6:ALL Y 11-20 ...表7-12:Y 21-30 ,表13-18:Y 31-40 ,表19-24:Y 41-50 ,表25-30:Y 51 -60 ,表31-36:Y 61-70 表1:X 21-30和表7,13,19,25,31 表2:X 31-40和表8,14,20,26,32 表3:X 41-50和表9,15,21,27,33 表4:X 51-60和表10,16,22,28,34 表5:X 61-70和表11,17,23,29,35 表6:X 71-80和表12,18,24,30,36

最终结果:

X Y Table 45 13 3 66 59 29 21 70 31 17 66 NA (there is no table for X lower than 21)

我是否应该使用If Else功能将“X”和“Y”中的数据分组到我的新“表格”中,范围从1到36或其他? 任何帮助将不胜感激! 谢谢!

头(数据)

value avg.temp X Y 1 0 6.69 45 13 2 0 6.01 48 14 3 0 7.35 39 15 4 0 5.86 45 15 5 0 6.43 42 16 6 0 5.68 48 16

I have a data set of "X" (values from 0 to 80) and "Y" (values from 0 to 80). I would like to create a new column "Table". I have 36 tables in mind: In groups of 6... They should be grouped according to:

Tables 1-6:ALL Y 11-20... Table 7-12:Y 21-30, Table 13-18:Y 31-40, Table 19-24:Y 41-50, Table 25-30:Y 51-60, Table 31-36:Y 61-70 Table 1: X 21-30 and Tables 7, 13, 19, 25, 31 Table 2: X 31-40 and Tables 8, 14, 20, 26, 32 Table 3: X 41-50 and Tables 9, 15, 21, 27, 33 Table 4: X 51-60 and Tables 10, 16, 22, 28, 34 Table 5: X 61-70 and Tables 11, 17, 23, 29, 35 Table 6: X 71-80 and Tables 12, 18, 24, 30, 36

End Result:

X Y Table 45 13 3 66 59 29 21 70 31 17 66 NA (there is no table for X lower than 21)

Should I be using the If Else function to group the data from the "X" and "Y" into my new "Table", ranging from 1 to 36 or something else? Any help will be appreciated! Thank you!

head(data)

value avg.temp X Y 1 0 6.69 45 13 2 0 6.01 48 14 3 0 7.35 39 15 4 0 5.86 45 15 5 0 6.43 42 16 6 0 5.68 48 16

最满意答案

我想你可以使用这样的东西。 如果您的数据框被称为df :

df$Table <- NA df$Table[df$X>=21 & df$X<=30 & df$Y>=11 & df$Y<=20] <- 1 df$Table[df$X>=31 & df$X<=40 & df$Y>=11 & df$Y<=20] <- 2 ...

I think you could use something like this. If your data frame is called df :

df$Table <- NA df$Table[df$X>=21 & df$X<=30 & df$Y>=11 & df$Y<=20] <- 1 df$Table[df$X>=31 & df$X<=40 & df$Y>=11 & df$Y<=20] <- 2 ...

更多推荐

本文发布于:2023-07-25 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1266996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   创建一个   类别   条件   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!