问题描述:

I have two sets of data as follows;

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

I am trying to either set column 1 of df to be new_col, or construct a new dataframe of new_col and columns 2:end of df

I have tried a bunch of things, but can't figure out the magic incantation to get it to work.... coming from something like python, this is driving be bonkers ;)

I have tried,

>> 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

Then, Eureka I thought! Almost there! at least I can add the col to the frame now...

>> 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

Excellent. So I just need to swap df and new_col around to create the frame I (almost) want! just a couple of steps away :)

>> dataframe(new_col.array(:,:),df)

error: Concatenating dataframes: use cat instead

error: called from

dataframe at line 568 column 5

Argh! How the what why?

>> 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

Help! - I've tried more permutations of various things than this, but it all ends the same way :(

网友答案:

I got it to work using its .cell property as opposed to .array you tried:

>> 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

However, this results in columns 1,3 and 4 as artefacts (presumably 1 and 4 is from an empty space character, and 3 is the previous 'ascending index' numbers). So I had to remove these to get my dataframe:

>> 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

I agree though, this package does seem pretty broken.

PS. The dataframe pkg is specific to octave. This question is not matlab-related. Matlab offers dataset and table classes (and I've also seen a datamatrix class, and a dataframe fileexchange one).

相关阅读:
Top