r-factor

Converting a factor to numeric without losing information R (as.numeric() doesn't seem to work) [duplicate]

主宰稳场 提交于 2019-11-27 05:19:34
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: R - How to convert a factor to an integer\numeric in R without a loss of information The following fact about the as.numeric() function has been brought to my attention > blah<-c("4","8","10","15") > blah [1] "4" "8" "10" "15" > blah.new<-as.factor(blah) > blah.new [1] 4 8 10 15 Levels: 10 15 4 8 > blah.new1<-as.numeric(blah.new) > blah.new1 [1] 3 4 1 2 When I convert a factor with levels 4, 8, 10, and 15 to a

Plotting with ggplot2: “Error: Discrete value supplied to continuous scale” on categorical y-axis

不问归期 提交于 2019-11-27 04:28:53
The plotting code below gives Error: Discrete value supplied to continuous scale What's wrong with this code? It works fine until I try to change the scale so the error is there... I tried to figure out solutions from similar problem but couldn't. This is a head of my data: > dput(head(df)) structure(list(`10` = c(0, 0, 0, 0, 0, 0), `33.95` = c(0, 0, 0, 0, 0, 0), `58.66` = c(0, 0, 0, 0, 0, 0), `84.42` = c(0, 0, 0, 0, 0, 0), `110.21` = c(0, 0, 0, 0, 0, 0), `134.16` = c(0, 0, 0, 0, 0, 0), `164.69` = c(0, 0, 0, 0, 0, 0), `199.1` = c(0, 0, 0, 0, 0, 0), `234.35` = c(0, 0, 0, 0, 0, 0), `257.19` = c

Change stringsAsFactors settings for data.frame

僤鯓⒐⒋嵵緔 提交于 2019-11-27 04:24:27
问题 I have a function in which I define a data.frame that I use loops to fill with data. At some point I get the Warning message: Warning messages: 1: In [<-.factor ( *tmp* , iseq, value = "CHANGE") : invalid factor level, NAs generated Therefore, when I define my data.frame, I'd like to set the option stringsAsFactors to FALSE but I don't understand how to do it. I have tried: DataFrame = data.frame(stringsAsFactors=FALSE) and also: options(stringsAsFactors=FALSE) What is the correct way to set

How do I get discrete factor levels to be treated as continuous?

狂风中的少年 提交于 2019-11-27 03:25:03
问题 I have a data frame with columns initially labeled arbitrarily. Later on, I want to change these levels to numerical values. The following script illustrates the problem. library(ggplot2) library(reshape2) m <- 10 n <- 6 nam <- list(c(),letters[1:n]) var <- as.data.frame(matrix(sort(rnorm(m*n)),m,n,F,nam)) dtf <- data.frame(t=seq(m)*0.1, var) mdf <- melt(dtf, id=c('t')) xs <- c(0.25,0.5,1.0,2.0,4.0,8.0) levels(mdf$variable) <- xs g <- ggplot(mdf,aes(variable,value,group=variable,colour=t)) g

geom_boxplot() from ggplot2 : forcing an empty level to appear

≯℡__Kan透↙ 提交于 2019-11-27 03:24:10
问题 I can't find a way to ask ggplot2 to show an empty level in a boxplot without imputing my dataframe with actual missing values. Here is reproducible code : # fake data dftest <- expand.grid(time=1:10,measure=1:50) dftest$value <- rnorm(dim(dftest)[1],3+0.1*dftest$time,1) # and let's suppose we didn't observe anything at time 2 # doesn't work even when forcing with factor(..., levels=...) p <- ggplot(data=dftest[dftest$time!=2,],aes(x=factor(time,levels=1:10),y=value)) p + geom_boxplot() #

Can we get factor matrices in R?

你离开我真会死。 提交于 2019-11-27 01:36:41
问题 It seems not possible to get matrices of factor in R. Is it true? If yes, why? If not, how should I do? f <- factor(sample(letters[1:5], 20, rep=TRUE), letters[1:5]) m <- matrix(f,4,5) is.factor(m) # fail. m <- factor(m,letters[1:5]) is.factor(m) # oh, yes? is.matrix(m) # nope. fail. dim(f) <- c(4,5) # aha? is.factor(f) # yes.. is.matrix(f) # yes! # but then I get a strange behavior cbind(f,f) # is not a factor anymore head(f,2) # doesn't give the first 2 rows but the first 2 elements of f #

Imported a csv-dataset to R but the values becomes factors

折月煮酒 提交于 2019-11-27 00:48:30
I am very new to R and I am having trouble accessing a dataset I've imported. I'm using RStudio and used the Import Dataset function when importing my csv-file and pasted the line from the console-window to the source-window. The code looks as follows: setwd("c:/kalle/R") stuckey <- read.csv("C:/kalle/R/stuckey.csv") point <- stuckey$PTS time <- stuckey$MP However, the data isn't integer or numeric as I am used to but factors so when I try to plot the variables I only get histograms, not the usual plot. When checking the data it seems to be in order, just that I'm unable to use it since it's

Factors in R: more than an annoyance?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 23:45:21
One of the basic data types in R is factors. In my experience factors are basically a pain and I never use them. I always convert to characters. I feel oddly like I'm missing something. Are there some important examples of functions that use factors as grouping variables where the factor data type becomes necessary? Are there specific circumstances when I should be using factors? Vince You should use factors. Yes they can be a pain, but my theory is that 90% of why they're a pain is because in read.table and read.csv , the argument stringsAsFactors = TRUE by default (and most users miss this

Subset data frame to include only levels of one factor that have values in both levels of another factor

早过忘川 提交于 2019-11-26 23:32:14
问题 I am working with a data frame that deals with numeric measurements. Some individuals have been measured several times, both as juveniles and adults. A reproducible example: ID <- c("a1", "a2", "a3", "a4", "a1", "a2", "a5", "a6", "a1", "a3") age <- rep(c("juvenile", "adult"), each=5) size <- rnorm(10) # e.g. a1 is measured 3 times, twice as a juvenile, once as an adult. d <- data.frame(ID, age, size) My goal is to subset that data frame by selecting the IDs that appear at least once as a

How to concatenate factors, without them being converted to integer level?

喜你入骨 提交于 2019-11-26 22:27:46
I was surprised to see that R will coerce factors into a number when concatenating vectors. This happens even when the levels are the same. For example: > facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer")) > facs [1] i want to be a factor not an integer Levels: a an be factor i integer not to want > c(facs[1 : 3], facs[4 : 5]) [1] 5 9 8 3 1 what is the idiomatic way to do this in R (in my case these vectors can be pretty large)? Thank you. 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