How to include columns in a DataFrame in Julia?

3

I have the following generic DataFrame with 100 rows:

using DataFrames

df = DataFrame(X=linspace(0.0, 2π, 100))
head(df)

    X
1   0.0
2   1.0471975511965976
3   2.0943951023931953
4   3.141592653589793

How to, for example, compute the sine of the values of the column X and insert in a new column SinX ?

    
asked by anonymous 02.02.2016 / 20:58

2 answers

4

To include the new column, simply assign the value (s) to this new column.

Example:

df[:SinX] = sin(df[:X])
head(df)

    X                   SinX
1   0.0                 0.0
2   0.06346651825433926 0.0634239196565645
3   0.12693303650867852 0.12659245357374926
4   0.19039955476301776 0.1892512443604102
5   0.25386607301735703 0.2511479871810792
6   0.3173325912716963  0.31203344569848707
    
02.02.2016 / 20:59
1

I think using the map function is more efficient because of composition, so you do not have to go through DataFrame twice, one initializing and the other assigning a functional value.

df[:error] = map((x,y) -> x-y , df[:A], df[:B])

Although specifically in your case it leads only to more code, I believe it leads to a better understanding.

df[:SinX] = map((x) -> sin(x), df[:X])

This means that for each entry of df[:X] , map to a new entry by applying the function sin(x)

    
15.11.2016 / 01:59