将两个数组转换为R中的一个数据帧

时间:2015-11-20 06:20:46

标签: arrays r shiny

我有两个来自postgreSQL数据库的数组,如下所示。

UILabel * myLabel = [[UILabel alloc] init];//used for whole string
myLabel.numberOfLines = 0;
NSString * myUserName = @"@LisaLisa";//used for userName

//add button on UserName
UIButton * muButton = [[UIButton alloc] init];
myLabel.text = [NSString stringWithFormat:@"%@", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];
//Here width is your Label's Width. Because we have to make sure that our, our label's width is not greater than our device's width and fontSize is label's fontSize
muButton.frame =  myLabel.frame;

myLabel.text = [NSString stringWithFormat:@"%@ is following you", myUserName];
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];//used for making dynamic height of label

//For changing color of UserName
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: myLabel.attributedText];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, myUserName.length)];
[myLabel setAttributedText: text];

两个数组都有相同数量的条目。我想将这些转换为数据框,因此我可以绘制i的图形。 我该怎么办?

我尝试//for setting the dynamic height of labels -(UILabel *)setDynamicLableFrame:(UILabel*)myLabel fontSize:(float)size Width:(float)Width { CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont fontWithName:REGULER_FONT size:size] constrainedToSize:CGSizeMake(300, 9999) lineBreakMode:NSLineBreakByWordWrapping]; CGRect newFrame = myLabel.frame; newFrame.size.height = possibleSize.height; if (possibleSize.width < Width) { newFrame.size.width = possibleSize.width; }else{ newFrame.size.width = Width; } myLabel.frame = newFrame; return myLabel; } 删除了{}和

iarray {9.467182035,9.252423958,9.179368178,9.142931845,9.118895803,9.098669713,9.093398102,9.092035392,9.091328028,9.090594437,9.090000456,9.089253543......keeps going on} varray {-1.025945126,-0.791203874,-0.506481774,-0.255416444,-0.028424464,0.188855034,0.390787963,0.579327969,0.761521769 ...keeps going on} 删除逗号。

2 个答案:

答案 0 :(得分:3)

假设你正在获得iarray&amp; varray as strings:

iarray = "{9.467182035,9.252423958,9.179368178,9.142931845}"
varray = "{-1.025945126,-0.791203874,-0.506481774,-0.255416444}"

n<-gsub("^\\{+(.+)\\}+$", '\\1', iarray)
n1 <- strsplit(n,",")
n1 <- unlist(n1)
df <- as.data.frame(n1)
n<-gsub("^\\{+(.+)\\}+$", '\\1', varray)
n2 <- strsplit(n,",")
n2 <- unlist(n2)

df <- cbind(df,n2)

答案 1 :(得分:0)

这似乎是正确使用eval(parse())的几个场合之一:

df<-list(iarray,varray)
df<-data.frame(lapply(df, 
    function(x) eval(parse(text=sub("\\}$",")",sub("^\\{","c(",x))))
))
names(df)<-c("iarray","varray")

我们只需将{替换为(,在开头添加ciarrayvarray成为命令行,以创建我们{ {1}}和parse