How to concatenate factors, without them being converted to integer level?
From the R Mailing list: unlist(list(facs[1 : 3], facs[4 : 5])) To ‘cbind’ factors, do data.frame(facs[1 : 3], facs[4 : 5])
From the R Mailing list: unlist(list(facs[1 : 3], facs[4 : 5])) To ‘cbind’ factors, do data.frame(facs[1 : 3], facs[4 : 5])
apply converts your data.frame to a character matrix. Use lapply: lapply(a, class) # $x1 # [1] “numeric” # $x2 # [1] “factor” # $x3 # [1] “factor” In second command apply converts result to character matrix, using lapply: a2 <- lapply(a, as.factor) lapply(a2, class) # $x1 # [1] “factor” # $x2 # [1] “factor” # … Read more
As mentioned in the comments, there cannot be a continuous scale on variable of the factor type. You could change the factor to numeric as follows, just after you define the meltDF variable. meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable] Then, execute the ggplot command ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) + scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, … Read more
data<-iris plot(data$Sepal.Length, data$Sepal.Width, col=data$Species) legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1) should do it for you. But I prefer ggplot2 and would suggest that for better graphics in R.
Use cut to do this in one step: dfc <- cut(df$x, breaks=c(0, 15, 45, 56, Inf)) str(dfc) Factor w/ 4 levels “(0,15]”,”(15,45]”,..: 3 4 3 2 2 4 2 2 4 4 … Once you are satisfied that the breaks are correctly specified, you can then also use the labels argument to relabel the levels: … Read more
Very short : levels are the input, labels are the output in the factor() function. A factor has only a level attribute, which is set by the labels argument in the factor() function. This is different from the concept of labels in statistical packages like SPSS, and can be confusing in the beginning. What you … Read more
Choose some columns to coerce to factors: cols <- c(“A”, “C”, “D”, “H”) Use lapply() to coerce and replace the chosen columns: data[cols] <- lapply(data[cols], factor) ## as.factor() could also be used Check the result: sapply(data, class) # A B C D E F G # “factor” “integer” “factor” “factor” “integer” “integer” “integer” # H … Read more
UPDATE 2: See Uwe’s answer which shows the new “tidyverse” way of doing this, which is quickly becoming the standard. UPDATE 1: Duplicated labels (but not levels!) are now indeed allowed (per my comment above); see Tim’s answer. ORIGINAL ANSWER, BUT STILL USEFUL AND OF INTEREST: There is a little known option to pass a … Read more
Since R version 2.12, there’s a droplevels() function. levels(droplevels(subdf$letters))