r-factor

Ignoring one of the factors of ggplot in R

帅比萌擦擦* 提交于 2019-12-10 10:58:47
问题 I use ggplot to plot my variable and attributes. I am using ggplot and factor by using the following code: require(ggplot2) require(reshape2) df <- data.frame(HMn25_30$avg,HMn25_30$h) df[3] = c("Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Outlier", "Outlier", "Outlier", "Outlier", "Outlier", "Outlier", "Normal", "Outlier", "Outlier", "Normal", "Normal", "Outlier", "Outlier", "Normal", "Normal" ) names(df)[1] <- 'Node 25' names(df)[3] <-

counting unique factors in r

梦想与她 提交于 2019-12-08 15:59:36
问题 I would like to know the number of unique dams which gave birth on each of the birth dates recorded. My data frame is similar to this one: dam <- c("2A11","2A11","2A12","2A12","2A12","4D23","4D23","1X23") bdate <- c("2009-10-01","2009-10-01","2009-10-01","2009-10-01", "2009-10-01","2009-10-03","2009-10-03","2009-10-03") mydf <- data.frame(dam,bdate) mydf # dam bdate # 1 2A11 2009-10-01 # 2 2A11 2009-10-01 # 3 2A12 2009-10-01 # 4 2A12 2009-10-01 # 5 2A12 2009-10-01 # 6 4D23 2009-10-03 # 7 4D23

In R, how can I access the first element of each level of a factor?

心已入冬 提交于 2019-12-08 15:47:02
问题 I have a data frame like this: n = c(2, 2, 3, 3, 4, 4) n <- as.factor(n) s = c("a", "b", "c", "d", "e", "f") df = data.frame(n, s) df n s 1 2 a 2 2 b 3 3 c 4 3 d 5 4 e 6 4 f and I want to access the first element of each level of my factor (and have in this example a vector containing a, c, e ). It is possible to reach the first element of one level, with df$s[df$n == 2][1] but it does not work for all levels: df$s[df$n == levels(n)] [1] a f How would you do that? And to go further, I’d like

How to deal with overlapping factor levels? (e.g. when producing tables and plots)

痴心易碎 提交于 2019-12-08 01:59:40
问题 I am facing a problem with a dataset which has overlapping factor levels . I would like to produce timelines, barplots and statistics by factor level - however, I want the factor levels to be equivocal. That means that observations belonging to more than one level should appear several times in a plot. Here is an example of how my data structure looks like: head <- c("ID","YEAR","BRAZIL","GERMANY","US","FRANCE") data <- data.frame(matrix(c(1,2000,1,0,0,0, 2,2010,0,1,1,0, 3,2011,0,1,0,0, 4

Counting values within levels

血红的双手。 提交于 2019-12-08 00:08:25
问题 I have a set of levels in R that I generate with cut , e.g. say fractional values between 0 and 1, broken down into 0.1 bins: > frac <- cut(c(0, 1), breaks=10) > levels(frac) [1] "(-0.001,0.1]" "(0.1,0.2]" "(0.2,0.3]" "(0.3,0.4]" "(0.4,0.5]" [6] "(0.5,0.6]" "(0.6,0.7]" "(0.7,0.8]" "(0.8,0.9]" "(0.9,1]" Given a vector v containing continuous values between [0.0, 1.0] , how do I count the frequency of elements in v that fall within each level in levels(frac) ? I could customize the number of

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

我与影子孤独终老i 提交于 2019-12-06 23:43:35
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 juvenile and at least once as an adult. Not sure how to do that..? The resulting dataframe would contain

Ignoring one of the factors of ggplot in R

对着背影说爱祢 提交于 2019-12-06 15:32:50
I use ggplot to plot my variable and attributes. I am using ggplot and factor by using the following code: require(ggplot2) require(reshape2) df <- data.frame(HMn25_30$avg,HMn25_30$h) df[3] = c("Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Outlier", "Outlier", "Outlier", "Outlier", "Outlier", "Outlier", "Normal", "Outlier", "Outlier", "Normal", "Normal", "Outlier", "Outlier", "Normal", "Normal" ) names(df)[1] <- 'Node 25' names(df)[3] <-'Results' df.m <- melt(df, names(df)[2:3], names(df)[1]) df.m$Results <- factor(df.m$Results) df.m$HMn25

How to deal with overlapping factor levels? (e.g. when producing tables and plots)

只愿长相守 提交于 2019-12-06 15:01:31
I am facing a problem with a dataset which has overlapping factor levels . I would like to produce timelines, barplots and statistics by factor level - however, I want the factor levels to be equivocal. That means that observations belonging to more than one level should appear several times in a plot. Here is an example of how my data structure looks like: head <- c("ID","YEAR","BRAZIL","GERMANY","US","FRANCE") data <- data.frame(matrix(c(1,2000,1,0,0,0, 2,2010,0,1,1,0, 3,2011,0,1,0,0, 4,2012,1,0,0,1, 5,2012,0,1,0,0, 6,2013,0,0,0,1), nrow=6, ncol=6, byrow=T)) names(data) <- head Obiously, a

Factorize a numeric variable with Greek expression in labels in R

梦想与她 提交于 2019-12-06 13:44:31
问题 Suppose the following data frame, I want to factorized var, and label numbers to Greek letters, from 1 to alpha, 2 to beta, 3 to gamma. But the following code does not work. var<-c(1,1,2,2,3,3) df<-as.data.frame(var) df$var<-factor(df$var, levels=c(1,2,3), labels=c("1"=expression(alpha), "2"=expression(beta), "3"=expression(gamma))) Why the final data frame is not greek letters but just text expressions? Can anyone help me on this? Thanks a lot. 回答1: Does your locale support these characters?

R: Why am I not getting type or class “factor” after converting columns to factor?

妖精的绣舞 提交于 2019-12-06 10:11:56
I have the following setup. df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb #"double" "double" apply(df, 2, class) # aa bb #"numeric" "numeric" Then I try to convert one of the columns to "factor". But as you can see below, I am not getting any "factor" type or classes. Am I doing anything wrong ? df[, 1] <- as.factor(df[, 1]) apply(df, 2, typeof) # aa bb #"character" "character" apply(df, 2, class) # aa bb #"character" "character" Sorry I felt my original answer badly written. Why did I put that "matrix of factors" in the very beginning? Here is a better try.