How to add a column to a data.frame in R?

r

#1

I have a data frame of salaries in which I want to add the new column to existing data salaries and I want to name the column experience and I want to give column value of experience name exp.

str(salaries)
'data.frame':	65 obs. of  2 variables:
 $ salary: num  58.8 34.8 163.7 70 55.5 ...
 $ degree: int  3 1 3 3 3 2 1 1 2 3 ...

#2

@harry,

you can create a new column in an existing data frame using

>salaries$experience=exp

you can also use cbind() function to add the new column to the existing dataframe

>experience=exp
>cbind(salaries, experience)

Hope this helps.


#3

Just to add to your commands, you can names the columns as -

colnames(dataset_name) <- c("firstcol","secondcol".....)