r-faq

How to debug “contrasts can be applied only to factors with 2 or more levels” error?

偶尔善良 提交于 2020-01-07 08:02:07
问题 Here are all the variables I'm working with: str(ad.train) $ Date : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels "Adelaide","Brisbane Lions",..: 1 1 1 1 1 1 1 1 1 1 ... $ Season : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ... $ Round : Factor w/ 28 levels "EF","GF","PF",..: 5 16 21 22 23 24 25 26 27 6 ... $ Score : int 137 82 84 96 110 99 122 124 49 111 ... $ Margin : int 69 18 -56 46 19 5 50 69 -26 29 ... $

How to debug “contrasts can be applied only to factors with 2 or more levels” error?

社会主义新天地 提交于 2020-01-06 01:56:32
问题 Here are all the variables I'm working with: str(ad.train) $ Date : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels "Adelaide","Brisbane Lions",..: 1 1 1 1 1 1 1 1 1 1 ... $ Season : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ... $ Round : Factor w/ 28 levels "EF","GF","PF",..: 5 16 21 22 23 24 25 26 27 6 ... $ Score : int 137 82 84 96 110 99 122 124 49 111 ... $ Margin : int 69 18 -56 46 19 5 50 69 -26 29 ... $

Convert data.frame column format from character to factor

╄→尐↘猪︶ㄣ 提交于 2019-12-27 10:44:48
问题 I would like to change the format (class) of some columns of my data.frame object ( mydf ) from charactor to factor . I don't want to do this when I'm reading the text file by read.table() function. Any help would be appreciated. 回答1: Hi welcome to the world of R. mtcars #look at this built in data set str(mtcars) #allows you to see the classes of the variables (all numeric) #one approach it to index with the $ sign and the as.factor function mtcars$am <- as.factor(mtcars$am) #another

Convert data.frame column format from character to factor

北慕城南 提交于 2019-12-27 10:43:20
问题 I would like to change the format (class) of some columns of my data.frame object ( mydf ) from charactor to factor . I don't want to do this when I'm reading the text file by read.table() function. Any help would be appreciated. 回答1: Hi welcome to the world of R. mtcars #look at this built in data set str(mtcars) #allows you to see the classes of the variables (all numeric) #one approach it to index with the $ sign and the as.factor function mtcars$am <- as.factor(mtcars$am) #another

Apply several summary functions on several variables by group in one call

安稳与你 提交于 2019-12-27 08:02:12
问题 I have the following data frame x <- read.table(text = " id1 id2 val1 val2 1 a x 1 9 2 a x 2 4 3 a y 3 5 4 a y 4 9 5 b x 1 7 6 b y 4 4 7 b x 3 9 8 b y 2 8", header = TRUE) I want to calculate the mean of val1 and val2 grouped by id1 and id2, and simultaneously count the number of rows for each id1-id2 combination. I can perform each calculation separately: # calculate mean aggregate(. ~ id1 + id2, data = x, FUN = mean) # count rows aggregate(. ~ id1 + id2, data = x, FUN = length) In order to

Apply several summary functions on several variables by group in one call

一世执手 提交于 2019-12-27 08:01:10
问题 I have the following data frame x <- read.table(text = " id1 id2 val1 val2 1 a x 1 9 2 a x 2 4 3 a y 3 5 4 a y 4 9 5 b x 1 7 6 b y 4 4 7 b x 3 9 8 b y 2 8", header = TRUE) I want to calculate the mean of val1 and val2 grouped by id1 and id2, and simultaneously count the number of rows for each id1-id2 combination. I can perform each calculation separately: # calculate mean aggregate(. ~ id1 + id2, data = x, FUN = mean) # count rows aggregate(. ~ id1 + id2, data = x, FUN = length) In order to

How to sort a dataframe by multiple column(s)

余生颓废 提交于 2019-12-25 04:37:21
问题 I want to sort a data.frame by multiple columns. For example, with the data.frame below I would like to sort by column z (descending) then by column b (ascending): dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), levels = c("Low", "Med", "Hi"), ordered = TRUE), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2)) dd b x y z 1 Hi A 8 1 2 Med D 3 1 3 Hi A 9 1 4 Low C 9 2 回答1: You can use the order() function directly without resorting to add-on tools -- see this simpler

Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()

烈酒焚心 提交于 2019-12-25 04:27:57
问题 I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...) the plot looks different to what it does if I pass the same variable using aes(x = mtcars$cyl, ...) . When I remove facet_grid(am ~ .) both graphs are the same again. The code below is modeled after the code in my project that generates the same behavior: library(dplyr) library(ggplot2) data = mtcars test.data = data

How to convert a factor to integer\numeric without loss of information?

不羁的心 提交于 2019-12-25 03:57:26
问题 When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers. f <- factor(sample(runif(5), 20, replace = TRUE)) ## [1] 0.0248644019011408 0.0248644019011408 0.179684827337041 ## [4] 0.0284090070053935 0.363644931698218 0.363644931698218 ## [7] 0.179684827337041 0.249704354675487 0.249704354675487 ## [10] 0.0248644019011408 0.249704354675487 0.0284090070053935 ## [13] 0.179684827337041 0.0248644019011408 0.179684827337041 ## [16] 0

Why are these numbers not equal?

╄→尐↘猪︶ㄣ 提交于 2019-12-25 01:34:50
问题 The following code is obviously wrong. What's the problem? i <- 0.1 i <- i + 0.05 i ## [1] 0.15 if(i==0.15) cat("i equals 0.15") else cat("i does not equal 0.15") ## i does not equal 0.15 回答1: General (language agnostic) reason Since not all numbers can be represented exactly in IEEE floating point arithmetic (the standard that almost all computers use to represent decimal numbers and do math with them), you will not always get what you expected. This is especially true because some values