rstringsubscriptionsuperscript

R Transform number/character to be a superscript or subscript


I want to superscript a number (or character, it doesnt matter) to an existing string.

This is what my initial dataframe looks like:

testframe = as.data.frame(c("A34", "B21", "C64", "D83", "E92", "F24"))
testframe$V2 = c(1,3,2,2,3,NA)
colnames(testframe)[1] = "V1"

  V1 V2
1 A34  1
2 B21  3
3 C64  2
4 D83  2
5 E92  3
6 F24  NA

What I want to do now is to use the V2 as a kind of "footnote", so any superscript or subscript (doesnt matter which one). When there is no entry in V2, then I just want to keep V1 as it is.

I found a similar question where I saw this answer:

> paste0("H", "\u2081", "O")
[1] "H₁O"

This is what I want, but my problem is that it has to be created automatically since I have way too many rows in my real dataframe.

I tried to add an extra column "V3" to enter the Superscripts and Subscripts Codes:

testframe$V3 = c("u2081", "u2083", "u2082", "u2082", "u2083", NA)

   V1 V2    V3
1 A34  1 u2081
2 B21  3 u2083
3 C64  2 u2082
4 D83  2 u2082
5 E92  3 u2083
6 F24 NA  <NA>

But when I try paste(testframe$V1, testframe$V3, sep = "\") it gives me an error. How can I use the \ in this case?


Solution

  • If the subscripts will always be digits 0 through 9, you can index into a vector of Unicode subscript digits:

    subscripts <- c(
      "\u2080",
      "\u2081",
      "\u2082",
      "\u2083",
      "\u2084",
      "\u2085",
      "\u2086",
      "\u2087",
      "\u2089"
    )
    
    testframe$V1 <- paste0(
      testframe$V1, 
      ifelse(
        is.na(testframe$V2), 
        "", 
        subscripts[testframe$V2 + 1]
      )
    )
    
    testframe
    
        V1 V2
    1 A34₁  1
    2 B21₃  3
    3 C64₂  2
    4 D83₂  2
    5 E92₃  3
    6  F24 NA