Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R function to create a new column with a value based on whether or not text is capitalized

$
0
0

I have a data frame df with 2 columns, x and y.

x <- c("ABC", "def", "GHI")
y <- c("jkl", "MNO", "pqr")
df <- as.data.frame(cbind(x,y), stringsAsFactors = FALSE)

I'd like to add a column z, where the value of z = x if x is all uppercase, and z = y if x is all lowercase.

e.g. column Z in this example would be

ABC
MNO
GHI

The code that makes sense to me is the following:

df$z <- ifelse(identical(z$x, toupper(z$x)), z$x, z$y)

but this is just giving me a column z with the value "jkl" in each row.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>