Mind reader game, and Unicode symbols
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Perhaps you’ve seen this Mind Reader game? Think of a two-digit positive whole number, such as 54. Subtract each of the two digits from your number, such as 54 – 5 – 4 = 45, and call 45 the RESULT. Examine the table of symbols below and find the SYMBOL that corresponds with your RESULT. Concentrate on the SYMBOL, and remember it. Then scroll down below, and I will read your mind to predict your SYMBOL.
Before I get to my prediction, let’s talk about Unicode symbols.
Unicode symbols
Unicode is a standard international system that assigns a unique Unicode number to each letter, digit, or symbol. This permits thousands of symbols to be written across different platforms, programs, and languages. An example of a Unicode number is “U+” followed by a hex number, such as U+1F499.
The table of symbols in the Mind Reader game is based on plotting a variety of Unicode symbols by printing their Unicode numbers.
A Unicode symbol is printed in R with its Unicode number, but beginning with a backslash to escape the U, and omitting the plus sign. For example, here is how to print a heart symbol.
print(“\U1F499”)
“💙”
Example with Hebrew letters
Let’s spell out the Hebrew word shalom , letter by letter, with each letter’s Unicode number, and then use the R paste command to paste the letters together.
shin <- "\U05E9" # "ש"
kamatz <- "\U05B7" # vowel as two perpendicular lines "ַ"
lamed <- "\U05DC" # "ל"
vav <- "\U05D5" # "ו"
final_mem <- "\U05DD" # "ם"
shin_with_kamatz <- "\U05E9\U05B7" # "שַ"
paste(shin_with_kamatz, lamed, vav, final_mem, sep = “”)
“שַלום”
Note that the letters are entered in the paste statement in order of first Hebrew letter, second Hebrew letter, etc., but they are printed in Hebrew right-to-left. Also, a better choice than vav is cholam, which is vav with a dot above it, “\U05BA”, but this doesn’t print for me.
My prediction
Here is my prediction of your symbol:
Want to play again? Think of another two-digit positive whole number, such as 54. Subtract each of the two digits from your number, such as 54 – 5 – 4 = 45, and call 45 the RESULT. Examine the table of symbols below and find the SYMBOL that corresponds with your RESULT. Concentrate on the SYMBOL, and remember it. Then scroll down below, and I will read your mind to predict your SYMBOL.
Plotting with Unicode characters
Tired of plotting points with those boring 25 pch symbols? You can use Unicode symbols, but you can’t simply use pch = . Here for no good reason I use a heart and a thumbs up.
library(ggplot2)
x <- seq(from=0,to=4, by=1)
y <- x^2
z <- exp(x)
df1 <- data.frame(x, y)
df2 <- data.frame(x, z)
heart <- "\U1F499"
thumbs_up <- "\U1F44D"
# Create a custom function to convert Unicode values to GeomPoint
custom_points <- function(data, mapping, ..., shape = heart) {
ggplot2::geom_point(data = data, mapping = mapping, …, shape = shape)
}
ggplot() +
custom_points(data = df1, aes(x = x, y = y, color = “y = x^2”), shape = heart, size=5) +
geom_line(data = df1, aes(x = x, y = y, color = “y = x^2”)) +
custom_points(data = df2, aes(x = x, y = z, color = “z = exp(x)”), shape = thumbs_up, size=5) +
geom_line(data = df2, aes(x = x, y = z, color = “z = exp(x)”)) +
ggtitle(“Plot with Unicode points”) +
labs(color = “Function”) +
theme(legend.position = c(0.15, 0.85),
plot.title = element_text(color=”black”, size=14, face=”bold”),
legend.text = element_text(color=”black”, size=10, face=”bold”))
My second prediction
Here is my second prediction:
Here is the code for the mind reader:
# start with arbitrary set of unicode symbols
description =
c(“a_bengali”,”a_gurmukhi”,”approximately_equal”,”biohazard”,”black_diamond”,
“black_heart”,”black_scissors”,”mercury”,
“mushroom”,”nya_gujarati”,
“section”,”snowflake”,
“snowman”,”teardrop_spoked_asterisk”,”thunderstorm”,
“umbrella_raindrops”,”white_cross”,”white_florette”,
“zhe_cyrillic”, “airplane”,
“black_right_arrow”,”black_telephone”,”blue_heart”,
“two_xs”,”hot_beverage”,”green apple”,”pill”,
“trophy”,”thumbs_up”)
unicode=
c(“\U0986″,”\U0A05″,”\U2248″,”\U2623″,”\U25C6″,”\U2665″,”\U2702″,”\U263F”,
“\U1F344″,”\U0A9E”,”\U00A7″,”\U2746″,
“\U26C4″,”\U273B”,”\U2608″,”\U2614″,”\U271E”,”\U2740″,
“\U04DC”,”\U2708″,”\U27A4″,”\U260E”,”\U1F499″,”\U1F9E0″,”\U2615″,”\U1F34F”,
“\U1F48A”,”\U1F3C6″,”\U1F44D”)
df_uni <- data.frame(cbind(description, unicode))
n <- nrow(df_uni)
x <- seq(1,n,1)
s <- sample(x, 1)
diag <- unicode[s]
diag
x <- x[-x[s]]
y <- sample(x, 100, replace = TRUE) # randomly choose 100 symbols
df <- data.frame(matrix(ncol = 20, nrow = 10))
for (i in seq(1, 19, by = 2)) {
df[, i] <- seq(99, 9, -10) - (i - 1) / 2 # row i, odd columns
df[, i + 1] <- y[((i - 1) / 2 * 10 + 1):((i - 1) / 2 * 10 + 10)] # row i, even columns
df[, i + 1] <- unicode[df[, i + 1]]
}
for (i in 1:10) {
df[i, 20 – 2*(i – 1)] <- diag
}
op <- par(bg = "thistle")
plot(x = c(0, 50*20), y = c(0, 50*10), type = “n”, xlab = “”, ylab = “”, xaxt = ‘n’,
yaxt = ‘n’, main = “Mind Reader # 1”, cex=2, font=2)
# Loop through each cell of the dataframe to draw rectangles and add text
for (i in 1:20) {
for (j in 1:10) {
# Determine the color based on whether the column index is odd or even
fill_color <- ifelse(i %% 2 == 1, "cornsilk", "lightblue")
# Draw rectangle with the determined color
rect(50*(i-1), 50*(10-j), 50*i, 50*(10-j+1), col = fill_color, border = “blue”)
# Add text in the center of the rectangle
text(50*(i-1) + 25, 50*(10-j) + 25, df[j, i], col = “navyblue”, cex = 1, font = 2)
}
}
# Restore original graphics parameters
par(op)
Hint on prediction
The prediction relies on a little algebra. As a hint, your original two-digit whole number is of the form, 10*T + U. What happens when you subtract the digits?
End
<
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.