I have a csv data file that looks like this:
> head(df)
# A tibble: 6 x 6
Name C H N O S
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 'Alanine' 3 7 1 2 0
2 'Arginine' 6 14 4 2 0
3 'Cysteine' 3 7 1 2 1
4 'Sucrose' 12 22 0 11 0
5 'Fructose' 6 12 0 6 0
6 'Ribose' 5 10 0 5 0
I wanted to paste all these different columns into one so that I will have one molecular formula for each row. I initially tried to do this by simply pasting values from each column:
> for (i in c(1:nrow(df)) {
df$formula[i] <- paste0("C", df$C[i], "H", df$H[i], "N", df$N[i],
"O", df$O[i], "S", df$S[i]) }
This works if there are no zeros in columns C through S, but if there are more than one zeros in columns, it will just paste zeroes like below, but I would like to have molecular formulas without zeroes.
> head(df$formula)
[1] "C3H7N1O2S0" "C6H14N4O2S0" "C3H7N1O2S1" "C12H22N0O11S0" "C6H12N0O6S0" "C5H10N0O5S0"
# what I want instead: "C3H7N1O2" "C6H14N4O2" "C3H7N1O2S1" "C12H22O11" "C6H12O6" "C5H10O5"
Would there be any other way to paste these columns only if the values are not zero? Or would pasting columns like this, then removing parts of the expression with zero be easier?