Rpy2子集数据框

时间:2016-03-11 04:43:08

标签: r python-3.x rpy2

我想将COL_TWO归为COL_ONE,其中' A'来自我在rdaData中从.rda文件加载的COL_ONE COL_TWO A 12 B 10 A 80 数据框。

rdaData:

import rpy2.robjects import r

r.load(path/to/files.rda)
substr_data = r.subset(r('rdaData'), COL_ONE == 'A', select = 'COL_TWO') 

代码:

COL_ONE == 'A'

运行COL_ONE not defined. 时出现此错误消息:

COL_ONE

我知道substr_data = r.subset(r('rdaData'), 'COL_ONE' == 'A', select = 'COL_TWO') 被视为Python变量而不是R.所以我尝试了:

substr_data = r.subset(r('rdaData'), r('rdaData$COL_ONE')== 'A', select = 'COL_TWO') 

COL_TWO

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("Left") var nav = appDelegate.window?.rootViewController as? UINavigationController nav = UINavigationController.init(rootViewController:vc ) hidesBottomBarWhenPushed = true let transition: CATransition = CATransition() let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.duration = 0.25 transition.timingFunction = timeFunc transition.type = kCATransitionPush transition.subtype = kCATransitionFromLeft //kCATransitionFromLeft nav!.view.layer.addAnimation(transition, forKey: kCATransition) appDelegate.window?.rootViewController = nav appDelegate.window?.makeKeyAndVisible() 都没有返回任何数据。我在r中尝试了代码,它返回了12和80.
我哪里出错了?

1 个答案:

答案 0 :(得分:0)

我不确定你是否还在这之后。这是我试过的:

import numpy as np
from rpy2.robjects import r
rnorm=r.rnorm
cbind=r.cbind
DataFrame=r('data.frame')

# Generating a matrix of some data
n_col =5
xa = r('matrix')(rnorm(30, 1), ncol = n_col)
ya = r('c(1, seq(5))')
ya = r('LETTERS[seq( from = 1, to = 5 )]')
x = cbind(xa,ya)

# Convert matrix to DataFrame (DF)
xa = DataFrame(x)
print xa

# Specify the DF column name used in selecting/slicing. eg.: 'COL_ONE' or 'X6':
new_col = 'X'+str(n_col+1)
# Slice (or subset()) the R DF:
substr_data = xa.rx(xa.rx2(new_col).ro == 'A', True)
print substr_data
# Specify the column name needed from the sliced DF. eg. 'COL_TWO' or 'X5'
print substr_data.rx2('X5')

输出:

> print xa
                 X1               X2                  X3                  X4
1  -0.4800320535369 2.68521681727218   0.623809846227243   0.810425086281231
2  1.54793994282147 1.39236531245408  -0.424155538823749  -0.242790003122539
3  1.39902476009121 1.23852817727937   0.934250526437131   0.789340066231089
4 0.903770245650284 2.06828848578716 -0.0602365472425763 -0.0602786816600411
5  2.06232894261465 1.39008580471573  -0.800324172073538   0.348292765491598
6 0.475607302003817 2.11744661073875    1.25253406148531  -0.276489137947105
                 X5 X6
1 -1.38012532899756  A
2 -1.70992738271866  B
3   1.7841406565434  C
4  -0.9296857462388  D
5 0.805075070886426  E
6 0.815799142148484  A

> print substr_data
                 X1               X2                X3                 X4
1  -0.4800320535369 2.68521681727218 0.623809846227243  0.810425086281231
6 0.475607302003817 2.11744661073875  1.25253406148531 -0.276489137947105
                 X5 X6
1 -1.38012532899756  A
6 0.815799142148484  A

> print substr_data.rx2('X5')
[1] -1.38012532899756 0.815799142148484
6 Levels: -0.9296857462388 -1.38012532899756 ... 1.7841406565434

最终答案是FactorVector ...我不知道如何从中获取数字(例如,例如,列表)但这种方法的DF切片部分似乎可以解决你的问题。

使用的链接:

  1. extract from R DF in rPy2
  2. generate list of letters
  3. how to subset R DF in R