I am trying to create a new data set where I have replaced the values in all rows for certain columns by applying a function to them. Ive done this with this code
so dividing each cell in columns 3,4,5 by the summation of the rows of 3,4,5.
This works fine. However, I have now lost all of my other columns in the data set. some with string value others with numerical value. any help would be much appreciated.
#step 1, have some data
df <- mtcars
# keep track of the names
orignames <- names(df)
# pick the parts to work on
process <- df[c(3:5)]
# set aside the parts not being worked on
notprocessed <- df[-c(3:5)]
# do work
df_new = as.data.frame(t(apply(process, 1, function(x) x/sum(x))))
# comine the work with the original parts not processed
final <- cbind(df_new,notprocessed)[,orignames]