Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from lowercase to uppercase all values in all character variables in dataframe

I have a mixed dataframe of character and numeric variables.

city,hs_cd,sl_no,col_01,col_02,col_03 Austin,1,2,,46,Female Austin,1,3,,32,Male Austin,1,4,,27,Male Austin,1,5,,20,Female Austin,2,2,,42,Female Austin,2,1,,52,Male Austin,2,3,,25,Male Austin,2,4,,22,Female Austin,3,3,,30,Female Austin,3,1,,65,Female 

I want to convert all the lower-case characters in the dataframe to uppercase. Is there any way to do this in one shot without doing it repeatedly over each character-variable?

like image 549
user702432 Avatar asked May 13 '13 07:05

user702432


People also ask

How will you change all the characters to upper case?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

How do I change everything to uppercase in R?

toupper() method in R programming is used to convert the lowercase string to uppercase string. Return: Returns the uppercase string.


2 Answers

Starting with the following sample data :

df <- data.frame(v1=letters[1:5],v2=1:5,v3=letters[10:14],stringsAsFactors=FALSE)    v1 v2 v3 1  a  1  j 2  b  2  k 3  c  3  l 4  d  4  m 5  e  5  n 

You can use :

data.frame(lapply(df, function(v) {   if (is.character(v)) return(toupper(v))   else return(v) })) 

Which gives :

  v1 v2 v3 1  A  1  J 2  B  2  K 3  C  3  L 4  D  4  M 5  E  5  N 
like image 60
juba Avatar answered Oct 23 '22 17:10

juba


From the dplyr package you can also use the mutate_all() function in combination with toupper(). This will affect both character and factor classes.

library(dplyr) df <- mutate_all(df, funs=toupper) 
like image 39
Trenton Hoffman Avatar answered Oct 23 '22 17:10

Trenton Hoffman