ybi
ybi

Reputation: 181

Convert a column in R data frame to lower case

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

Upvotes: 13

Views: 58238

Answers (4)

andschar
andschar

Reputation: 3973

The data.table way:

require(data.table)

setDT(RecordsWithIssues)
RecordsWithIssues[ , OTHERCOUNTRY := tolower(OTHERCOUNTRY) ]

Upvotes: 1

smitah19
smitah19

Reputation: 11

Here is my data

enter image description here

To convert to lower

head(rounds2)

rounds2_lower <-data.frame(tolower(rounds2$company_permalink))

cbind- to bind both the dataframes to a final data frame

rounds2_final <- cbind(rounds2,rounds2_lower)

give appropriate names to the column

> names(rounds2_final)
[1] "company_permalink"       "funding_round_permalink"     
[3] ""tolower.rounds2.company_permalink."

names(rounds2_final)[3] <- "company_permalink_lower"

Upvotes: 0

Rentrop
Rentrop

Reputation: 21497

require(tidyverse)

RecordsWithIssues %>% mutate(OTHERCOUNTRY = tolower(OTHERCOUNTRY))

Upvotes: 10

akrun
akrun

Reputation: 887118

We need to extract with [[ as the dataset also include the tbl_df class

RecordsWithIssues[[3]] <- tolower(RecordsWithIssues[[3]])

Or $

RecordsWithIssues$OTHERCOUNTRY <- tolower(RecordsWithIssues$OTHERCOUNTRY)

Upvotes: 20

Related Questions