将R数据框中的列转换为小写

时间:2017-07-27 17:08:37

标签: r

这是我的数据

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

我做以下

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", "
> 

如您所见,数据框现在有一个向量而不是单个文本值。如何在不获取文本的情况下简单地转换字符串

4 个答案:

答案 0 :(得分:8)

我们需要使用[[提取,因为数据集还包含tbl_df

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

$

RecordsWithIssues$OTHERCOUNTRY <- tolower(RecordsWithIssues$OTHERCOUNTRY)

答案 1 :(得分:2)

require(tidyverse)

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

答案 2 :(得分:0)

这是我的数据

enter image description here

要转换为较低的

head(rounds2)

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

cbind-将两个数据帧都绑定到最终数据帧

rounds2_final <- cbind(rounds2,rounds2_lower)

为该列提供适当的名称

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

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

答案 3 :(得分:0)

data.table方式:

require(data.table)
RecordsWithIssues[ , OTHERCOUNTRY := tolower(OTHERCOUNTRY) ]