lundi 29 juillet 2019

How do you exclude values when creating a string when setting up initial conditions?

I'm trying to combine columns in my data frame so that they give me a certain string. I have columns titled as "C", "H", "O", "N", and "S" as elements. Within those columns are listed the number of elements within that molecule, but I want to exclude some elements depending on their value. For example when there is no Oxygens the value is 0, so i want to exclude this when combining the elements to make a string.

#This is a portion of my data frame titled data4a
#C H O N S
#3 4 0 0 1
#7 5 4 1 0

#The code I have is 
data4a$NewComp = paste("C",data4a$Total.C,"H", data4a$NewH, "O", data4a$O, "N", data4a$N, "S", data4a$S, sep = "")

#This code gives me this
#C H O N S NewComp
#3 4 0 0 1 C3H4O0N0S1
#7 5 4 1 0 C7H5O4N1S0

#I expect to see something like this when I print my results
#C H O N S NewComp
#3 4 0 0 1 C3H4S1
#7 5 4 1 0 C7H5O4N

#I want values of zero to be excluded from the string created

Aucun commentaire:

Enregistrer un commentaire