Dear R-List,
#I have a df with the first two cols formatted as factor.
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))
# now I want to format both factor VARs as character
# I tried
factor.id<-names(dfx[sapply(dfx,is.factor)])
chr.names<-which(names(dfx)%in% factor.id)
dfx[ , chr.names]<-as.character(dfx[ , chr.names])
# which gives me
str(dfx)
'data.frame': 29 obs. of 3 variables: $ group: chr "c(1, 1, 1, 1,
1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)" "c(2, 2,
1, 1, 1,
1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2)"
"c(1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
3)" "c(2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2,
1, 1, 1,
1, 1, 2, 2, 2)" ... $ sex : chr "c(2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1,
2, 2, 2,
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2)" "c(1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)" "c(2, 2, 1, 1,
1, 1, 2,
1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2)"
"c(1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
3)"
... $ age : num 38.5 18 33.5 26 22.5 ...
#though I was hoping for something like
'data.frame': 29 obs. of 3 variables:
$ group: chr "A" "A" "A" "A" ...
$ sex : chr "M" "F" "F" "M" ...
$ age : num 21.3 35.2 53.8 21 23.6 ...
#What is wrong with my code?
#Thank you for any help
Best wishes
Alain
[[alternative HTML version deleted]]
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))> dfxgroup 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> dfxgroup 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.
Of course you could have created them as character vectors in the first
place:
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),
stringsAsFactors=FALSE
)
But if that is not possible in your context, then I would suggest this:
for (nm in names(dfx))
if (is.factor(dfx[[nm]])) dfx[[nm]] <- as.character(dfx[[nm]])
It's clear and simple.
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 2/26/15, 2:08 AM, "Alain D." <dialvac-r at yahoo.de> wrote:
>Dear R-List,
>
>#I have a df with the first two cols formatted as factor.
>
>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))
>
># now I want to format both factor VARs as character
># I tried
>
>factor.id<-names(dfx[sapply(dfx,is.factor)])
>chr.names<-which(names(dfx)%in% factor.id)
>
>dfx[ , chr.names]<-as.character(dfx[ , chr.names])
># which gives me
>str(dfx)
>'data.frame': 29 obs. of 3 variables: $ group: chr "c(1, 1, 1,
1, 1, 1,
>1, 1, 2,
>2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)" "c(2,
2, 1,
>1, 1,
>1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2,
2)"
>"c(1,
>1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
>3, 3, 3,
>3)" "c(2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1,
2, 1,
>1, 1,
>1, 1, 2, 2, 2)" ... $ sex : chr "c(2, 2, 1, 1, 1, 1, 2, 1, 1, 2,
2, 1, 2,
>2, 2,
>1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2)" "c(1, 1, 1, 1, 1, 1, 1,
1, 2,
>2, 2,
>2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)" "c(2, 2, 1,
1, 1,
>1, 2,
>1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2)"
"c(1,
>1, 1,
>1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
>3, 3)"
>... $ age : num 38.5 18 33.5 26 22.5 ...
>
>#though I was hoping for something like
>
>'data.frame': 29 obs. of 3 variables:
>$ group: chr "A" "A" "A" "A" ...
>$ sex : chr "M" "F" "F" "M" ...
>$ age : num 21.3 35.2 53.8 21 23.6 ...
>
>#What is wrong with my code?
>#Thank you for any help
>
>Best wishes
>
>Alain
>
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.