我想根据另一列“ dim”中的间隔在数据框中创建一个新列
例如:
我的数据集是:
df1
id dim
1 25
2 34
3 60
4 65
5 80
6 82
7 90
8 95
9 110
10 120
I would like the follow data set below using the interval by 20 (my column begin with 25 for a new column x
factors: 25:44 = 1 45=64= 2 and so on...
df2
id dim x
1 25 1
2 34 1
3 60 2
4 65 3
5 80 3
6 82 3
7 90 4
8 95 4
9 110 5
10 120 5
有人可以帮我吗?
您可以使用floor
一些数学方法来做到这一点:
df <- data.frame(id = 1:10, dim = c(25, 34, 60, 65, 80, 82, 90, 95, 110, 120))
df$x <- floor((df$dim - min(df$dim)) / 20) + 1
# id dim x
#1 1 25 1
#2 2 34 1
#3 3 60 2
#4 4 65 3
#5 5 80 3
#6 6 82 3
#7 7 90 4
#8 8 95 4
#9 9 110 5
#10 10 120 5
减去最小的条目df$dim
以开始您的第一类;除以20得到间隔大小; 向下取整,并加1以适当地向上移动所有内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句