重构data.frame列值 [英] refactor data.frame column values

查看:122
本文介绍了重构data.frame列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个noob的问题。
我需要帮助如何循环我的数据框。这是一个示例数据。

  a<  -  c (10:29); 
b <-C(40:59);
e< - rep(1,20);
test< - data.frame(a,b,e)

我需要对于



的列a列中的值使用以下条件操纵列e:



a< = 15,e= 1,



a> 15& < 20,e= 2



a> 20& < 25,e= 3



a> 25& < 30,e= 4等等看起来像这样

  result<  -  cbind(a,b,rep 1:4,each = 5))

我的实际数据框架超过100k长。如果你可以在这里排序我,会很棒。

解决方案

  data.frame ,b,e =(1:4)[cut(a,c(-Inf,15,20,25,30))])

更新:



Greg's 注释提供了一个更直接的解决方案,无需通过子集化整数向量,其中一个因子从 cut 返回。

  data.frame(a,b,e = findInterval(a,c(-Inf,15,20,25,30))) 


Sorry guys if this is a noob question. I need help on how to loop over my dataframe.Here is a sample data.

a <- c(10:29);
b <- c(40:59);
e <- rep(1,20);
test <- data.frame(a,b,e)

I need to manipulate column "e" using the following criteria for values in column "a"

for all values of

"a" <= 15, "e" = 1,

"a" > 15 & < 20, "e" = 2

"a" > 20 & < 25, "e" = 3

"a" > 25 & < 30, "e" = 4 and so on to look like this

result <- cbind(a,b,rep(1:4, each=5))

My actual data frame is over 100k long. Would be great if you could sort me out here.

解决方案

data.frame(a, b, e=(1:4)[cut(a, c(-Inf, 15, 20, 25, 30))])

Update:

Greg's comment provides a more direct solution without the need to go via subsetting an integer vector with a factor returned from cut.

data.frame(a, b, e=findInterval(a, c(-Inf, 15, 20, 25, 30)))

这篇关于重构data.frame列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆