how to add column into existing table?

I am trying to add a new column, if someone can please help me with this. My code looks like:

df <- read.csv("F:/mfa_file.csv", stringsAsFactors = F)
df1 <- subset(df, select = c(1,23:43))
df2 <- df1[,-1]
df3 <- tibble::add_column(flavorliking = , .after = "cauliflower")

You are not passing a param to add_column that would tell the dataframe to add the colum to. df2 presumably, also if you want it to contain NA values then you need to set them to be what flavorliking contains, there's nothing to the right of your equal sign.
You can take a look at the documentation easily by typing question mark followed by function name

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

df3 <- tibble::add_column(df2,flavorliking = NA, .after = "cauliflower")

of course this will fail if you don't have a 'cauliflower' column in df2. (your screenshot doesnt show that it does)

1 Like

@nirgrahamuk thanks. I saw example, it shows (x = 1:3, y = 3:1), but my table is different, so how i should specify x and y?

Thanks @nirgrahamuk. So now i have a column in my table. Can i specify values manually for the rows under this column?

yes, instead of NA, list them as a vector using the c() syntax

1 Like