For example say you create a Julia DataFrame like so with 20 columns:
y=convert(DataFrame, randn(10,20))
How do you convert the column names (:x1 ... :x20)
to something else, like (:col1, ..., :col20)
for example, all at once?
We can use: Symbol : select(df, :col) String : select(df, "col") Integer : select(df, 1)
rename() is the method available in the dplyr library which is used to change the multiple columns (column names) by name in the dataframe. The operator – %>% is used to load the renamed column names to the dataframe. At a time it will change single or multiple column names.
Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.
You might find the names!
function more concise:
julia> using DataFrames julia> df = DataFrame(x1 = 1:2, x2 = 2:3, x3 = 3:4) 2x3 DataFrame |-------|----|----|----| | Row # | x1 | x2 | x3 | | 1 | 1 | 2 | 3 | | 2 | 2 | 3 | 4 | julia> names!(df, [symbol("col$i") for i in 1:3]) Index([:col2=>2,:col1=>1,:col3=>3],[:col1,:col2,:col3]) julia> df 2x3 DataFrame |-------|------|------|------| | Row # | col1 | col2 | col3 | | 1 | 1 | 2 | 3 | | 2 | 2 | 3 | 4 |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With