Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a column in R data frame to lower case

Tags:

r

Here is my data

summary(RecordsWithIssues)
ID             OTHERSTATE        OTHERCOUNTRY      
Length:373         Length:373         Length:373        
Class :character   Class :character   Class :character  
Mode  :character   Mode  :character   Mode  :character

> head(RecordsWithIssues)
# A tibble: 6 × 3
                  ID OTHERSTATE      OTHERCOUNTRY
               <chr>      <chr>             <chr>
1 0034000001uhro2AAA         MO              <NA>
2 0034000001uhyOsAAI       <NA>          reseller
3 0034000001uhyPJAAY       <NA>          AECbytes
4 0034000001uhyPZAAY       <NA>            Friend
5 0034000001uhyPeAAI       <NA>            client
6 0034000001uhyPnAAI       <NA>     good energies

I do the following

RecordsWithIssues[,3]=tolower(RecordsWithIssues[,3])
RecordsWithIssues[1,3]
# A tibble: 1 × 1
                                                                                                             OTHERCOUNTRY
                                                                                                                    <chr>
1 c(na, "reseller", "aecbytes", "friend", "client", "good energies", "boss", "friend", "linkedin", "aecbytes", "
> 

As you can see data frame now has a vector instead of single text value. How can I simply convert the string without getting the text

like image 251
ybi Avatar asked Jul 27 '17 17:07

ybi


People also ask

How do I change values to lowercase in R?

To convert an uppercase string to lowercase in R, use the tolower() method. It takes a string as an argument and returns the lowercase version. The tolower() method changes the case of a string to the lower.

How do I convert a column to a character in R?

To convert all columns of the data frame into the character we use apply() function with as. character parameter. The lapply() function applies the given function to the provided data frame.

How do I change a column in a Dataframe in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.


2 Answers

require(tidyverse)

RecordsWithIssues %>% mutate(OTHERCOUNTRY = tolower(OTHERCOUNTRY))
like image 125
Rentrop Avatar answered Sep 27 '22 20:09

Rentrop


The data.table way:

require(data.table)

setDT(RecordsWithIssues)
RecordsWithIssues[ , OTHERCOUNTRY := tolower(OTHERCOUNTRY) ]
like image 44
andschar Avatar answered Sep 27 '22 18:09

andschar