[R] format selected columns in dataframe as character

JS Huang js.huang at protective.com
Thu Feb 26 14:54:26 CET 2015


Try as.character like the following shows.

> dfx <- data.frame( 
+   group = c(rep('A', 8), rep('B', 15), rep('C', 6)), 
+   sex = sample(c("M", "F"), size = 29, replace = TRUE), 
+   age = runif(n = 29, min = 18, max = 54))
> dfx
   group sex         age
1      A   M 41.35554346
2      A   F 47.73245469
3      A   F 42.97870796
4      A   M 52.51180396
5      A   F 46.72228944
6      A   M 48.64668630
7      A   M 36.07894452
8      A   M 26.96805121
9      B   M 30.67208692
10     B   M 45.09322672
11     B   M 31.86601692
12     B   F 53.28861780
13     B   M 27.74271305
14     B   F 52.05845066
15     B   F 18.94612430
16     B   M 48.66673378
17     B   F 53.07004762
18     B   F 48.15222416
19     B   M 32.17737802
20     B   M 37.02122907
21     B   M 39.31442046
22     B   M 27.90392578
23     B   M 44.70562356
24     C   F 53.43127126
25     C   M 49.85362283
26     C   M 40.40779822
27     C   F 31.41189728
28     C   M 47.49351314
29     C   M 46.34333618
> summary(dfx)
 group  sex         age          
 A: 8   F:10   Min.   :18.94612  
 B:15   M:19   1st Qu.:32.17738  
 C: 6          Median :44.70562  
               Mean   :41.46947  
               3rd Qu.:48.64669  
               Max.   :53.43127  
> dfx$group <- *as.character*(dfx$group)
> summary(dfx)
    group           sex         age          
 Length:29          F:10   Min.   :18.94612  
 Class :character   M:19   1st Qu.:32.17738  
 Mode  :character          Median :44.70562  
                           Mean   :41.46947  
                           3rd Qu.:48.64669  
                           Max.   :53.43127  
> dfx
   group sex         age
1      A   M 41.35554346
2      A   F 47.73245469
3      A   F 42.97870796
4      A   M 52.51180396
5      A   F 46.72228944
6      A   M 48.64668630
7      A   M 36.07894452
8      A   M 26.96805121
9      B   M 30.67208692
10     B   M 45.09322672
11     B   M 31.86601692
12     B   F 53.28861780
13     B   M 27.74271305
14     B   F 52.05845066
15     B   F 18.94612430
16     B   M 48.66673378
17     B   F 53.07004762
18     B   F 48.15222416
19     B   M 32.17737802
20     B   M 37.02122907
21     B   M 39.31442046
22     B   M 27.90392578
23     B   M 44.70562356
24     C   F 53.43127126
25     C   M 49.85362283
26     C   M 40.40779822
27     C   F 31.41189728
28     C   M 47.49351314
29     C   M 46.34333618
> dfx$sex <- *as.character*(dfx$sex)
> summary(dfx)
    group               sex                 age          
 Length:29          Length:29          Min.   :18.94612  
 Class :character   Class :character   1st Qu.:32.17738  
 Mode  :character   Mode  :character   Median :44.70562  
                                       Mean   :41.46947  
                                       3rd Qu.:48.64669  
                                       Max.   :53.43127  
> class(dfx$group)
[1] "character"
> dfx$group
 [1] "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
"B" "B" "B" "B" "B" "C"
[25] "C" "C" "C" "C" "C"
> class(dfx$sex)
[1] "character"
> dfx$sex
 [1] "M" "F" "F" "M" "F" "M" "M" "M" "M" "M" "M" "F" "M" "F" "F" "M" "F" "F"
"M" "M" "M" "M" "M" "F"
[25] "M" "M" "F" "M" "M"
> 



--
View this message in context: http://r.789695.n4.nabble.com/format-selected-columns-in-dataframe-as-character-tp4703863p4703878.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list