在八度数据帧中设置列值

时间:2016-11-08 16:29:55

标签: matlab dataframe octave

我有两组数据如下;

file a:
 1476356687
 1476356689
 1476356690

file b:
 "2016-10-13 12:04:47.706193",1000,516130
 "2016-10-13 12:04:49.225305",2000,516130
 "2016-10-13 12:04:50.439240",3000,516130

 new_col=dataframe("file_a")
 df=dataframe("file_b")


>> size(new_col)
ans =

   3     1

>> size(df)
ans =

   3     3

>> new_col(1:3,:)
ans = dataframe with 3 rows and 1 columns
_1    new_col
Nr     double
 1 1476356687
 2 1476356689
 3 1476356690
>> df(1:3,:)
ans = dataframe with 3 rows and 3 columns
Src: import_nbu_op
_1                         X1     X2     X3
Nr                       char double double
 1 2016-10-13 12:04:47.706193   1000 516130
 2 2016-10-13 12:04:49.225305   2000 516130
 3 2016-10-13 12:04:50.439240   3000 516130

我正在尝试将df的第1列设置为new_col,或者构建new_col的新数据帧和第2列:df的结尾

我已经尝试了很多东西,但是无法弄清楚它是如何起作用的....它来自像python这样的东西,这是疯狂的;)

我试过了,

>> df=dataframe(new_col,df)
error: Concatenating dataframes: use cat instead
error: called from
    dataframe at line 568 column 5
>> df=cat(new_col,df)
error: Incorrect call to cat
error: called from
    cat at line 197 column 7
>> df=cat(1,new_col,df)
error: Different number of columns in dataframes
error: called from
    cat at line 50 column 11
>> df=cat(2,new_col,df)
df(1): out of bound 0
error: called from
    display at line 66 column 7
>> df=cat(0,new_col,df)
error: Incorrect call to cat
error: called from
    cat at line 197 column 7
    display at line 66 column 7
>> cat(2,1,new_col,df)
error: Different number of rows in dataframes
error: called from
    cat at line 109 column 11
>> cat(2,2,new_col,df)
error: Different number of rows in dataframes
error: called from
    cat at line 109 column 11
>> horzcat(new_col,df)
error: df(2): out of bound 1
error: called from
    display at line 66 column 7
>> df(:,1)=new_col
error: cast: TYPE must be a string
error: called from
    cast at line 67 column 5
    df_matassign at line 452 column 31
    subsasgn at line 217 column 10
>> dataframe(df,new_col)
error: Concatenating dataframes: use cat instead
error: called from
    dataframe at line 568 column 5

然后,Eureka我想!差不多了!至少我现在可以将col添加到框架中......

>> dataframe(df,new_col.array(:,:))
ans = dataframe with 3 rows and 4 columns
Src: import_nbu_op
 _1                         X1     X2     X3          X
 Nr                       char double double     double
  1 2016-10-13 12:04:47.706193   1000 516130 1476356687
  2 2016-10-13 12:04:49.225305   2000 516130 1476356689
  3 2016-10-13 12:04:50.439240   3000 516130 1476356690

优异。所以我只需要交换df和new_col来创建我(几乎)想要的框架!只有几步之遥:))

>> dataframe(new_col.array(:,:),df)
error: Concatenating dataframes: use cat instead
error: called from
    dataframe at line 568 column 5

哎呀!怎么回事?

>> cat(1,new_col,df(:,2:end))
error: Different number of columns in dataframes
error: called from
    cat at line 50 column 11
>> cat(2,new_col,df(:,2:end))
error: df(2): out of bound 1
error: called from
    display at line 66 column 7
>> horzcat(new_col,df(:,2:end))
error: df(2): out of bound 1

帮助! - 我尝试过比这更多的各种事情的排列,但这一切都以同样的方式结束:(

1 个答案:

答案 0 :(得分:1)

我使用.cell属性而不是您尝试的.array让它工作:

>> concatenated = dataframe(cat(2, new_col.cell, df.cell))
concatenated = dataframe with 3 rows and 7 columns                              
_1 unnamed          X unnamed1 unnamed2                         X1     X2     X3
Nr    char     double   double     char                       char double double
 1         1.4764e+09        1          2016-10-13 12:04:47.706193   1000 516130
 2         1.4764e+09        2          2016-10-13 12:04:49.225305   2000 516130
 3         1.4764e+09        3          2016-10-13 12:04:50.439240   3000 516130

然而,这导致列1,3和4作为假象(可能1和4来自空白字符,3是前面的'升序索引'数字)。所以我不得不删除这些来获取我的数据帧:

>> concatenated(:,[1,3,4]) = []
concatenated = dataframe with 3 rows and 4 columns    
_1          X                         X1     X2     X3
Nr     double                       char double double
 1 1.4764e+09 2016-10-13 12:04:47.706193   1000 516130
 2 1.4764e+09 2016-10-13 12:04:49.225305   2000 516130
 3 1.4764e+09 2016-10-13 12:04:50.439240   3000 516130

我同意,但这个套装看起来确实很糟糕。

PS。 dataframe pkg特定于八度音阶。这个问题与matlab无关。 Matlab提供datasettable类(我还看过datamatrix类,dataframe文件交换类。)