r-factor

How to convert factor format to numeric format in R without changing the values? [duplicate]

混江龙づ霸主 提交于 2019-12-06 04:53:14
问题 This question already has answers here : How can i convert a factor column that contains decimal numbers to numeric? (4 answers) Closed 6 years ago . Below is dataframe df1 of which I want to convert column "V2" from factor format to numeric without changing the current values (0 ; 0 ; 8,5 ; 3). df1= V1 V2 V3 X2 X3 4470 2010-03-28 0 A 21.53675 0 4471 2010-03-29 0 A 19.21611 0 4472 2010-03-30 8,5 A 21.54541 0 4473 2010-03-31 3 A NA NA Since column "V2" is in factor format I first convert it to

How to make the levels of a factor in a data frame consistent across all columns?

大城市里の小女人 提交于 2019-12-04 19:30:50
问题 I have a data frame with 5 different columns: Test1 Test2 Test3 Test4 Test5 Sample1 PASS PASS FAIL WARN WARN Sample2 PASS PASS FAIL PASS WARN Sample3 PASS FAIL FAIL PASS WARN Sample4 PASS FAIL FAIL PASS WARN Sample5 PASS WARN FAIL WARN WARN In each column, each level is assigned a different factor. In column 1, "PASS" is 1. In column 2, "PASS" is 2 and "FAIL is 1. In column 3, "FAIL" is 1. In column 4, "PASS" is 1 and "WARN" is 2. In column 5, "WARN" IS 1. It is doing it by alphabetical order

Factorize a numeric variable with Greek expression in labels in R

99封情书 提交于 2019-12-04 19:16:51
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. Does your locale support these characters? Does '\u03b1' print an alpha character? If not, you'll need to change your encoding. E.g., Sys.setlocale(

How to convert factor format to numeric format in R without changing the values? [duplicate]

冷暖自知 提交于 2019-12-04 09:03:44
This question already has answers here : How can i convert a factor column that contains decimal numbers to numeric? (4 answers) Closed 6 years ago . Below is dataframe df1 of which I want to convert column "V2" from factor format to numeric without changing the current values (0 ; 0 ; 8,5 ; 3). df1= V1 V2 V3 X2 X3 4470 2010-03-28 0 A 21.53675 0 4471 2010-03-29 0 A 19.21611 0 4472 2010-03-30 8,5 A 21.54541 0 4473 2010-03-31 3 A NA NA Since column "V2" is in factor format I first convert it to character format: df1[,2]=as.character(df1[,2]) Then I try to convert "V2" to numeric format: df1[,2]

R proportion confidence interval factor

落花浮王杯 提交于 2019-12-04 07:19:26
I am trying to summarise data from a household survey and as such most of my data is categorical (factor) data. I was looking to summarise it with plots of frequencies of responses to certain questions (e.g., a bar plot of percentages of households answering certain questions, with error bars showing confidence intervals). I found this excellent tutorial which I had thought was the answer to my prayers ( http://www.cookbook-r.com/Manipulating_data/Summarizing_data/ ) but turns out this is only going to help with continuous data. What I need is something similar that will allow me to calculate

How to make the levels of a factor in a data frame consistent across all columns?

扶醉桌前 提交于 2019-12-03 13:11:24
I have a data frame with 5 different columns: Test1 Test2 Test3 Test4 Test5 Sample1 PASS PASS FAIL WARN WARN Sample2 PASS PASS FAIL PASS WARN Sample3 PASS FAIL FAIL PASS WARN Sample4 PASS FAIL FAIL PASS WARN Sample5 PASS WARN FAIL WARN WARN In each column, each level is assigned a different factor. In column 1, "PASS" is 1. In column 2, "PASS" is 2 and "FAIL is 1. In column 3, "FAIL" is 1. In column 4, "PASS" is 1 and "WARN" is 2. In column 5, "WARN" IS 1. It is doing it by alphabetical order I need "PASS" be 1 in all columns, "WARN" to be 2 in all columns, and "FAIL" 3 in all columns, so that

list all factor levels of a data.frame

ぃ、小莉子 提交于 2019-12-03 11:06:44
问题 with str(data) I get the head of the levels (1-2 values) fac1: Factor w/ 2 levels ... : fac2: Factor w/ 5 levels ... : fac3: Factor w/ 20 levels ... : val: num ... with dplyr::glimpse(data) I get more values, but no infos about number/values of factor-levels. Is there an automatic way to get all level informations of all factor vars in a data.frame? A short form with more info for levels(data$fac1) levels(data$fac2) levels(data$fac3) or more precisely a elegant version for something like for

Reorder factor levels by day of the week in R

坚强是说给别人听的谎言 提交于 2019-12-03 06:56:24
问题 I have the following data.frame in R: > daily DoW Duration 1 Friday 14.0000000000000 2 Monday 21.0000000000000 3 Saturday 12.0000000000000 4 Thursday 28.0000000000000 5 Tuesday 12.0000000000000 6 Wednesday 91.0000000000000 7 Sunday 20.0000000000000 I'd like to change the order of the factor levels so that the weeks are in (US) day-of-week order. It looks like I can do this in a slow, puzzling way with relevel() . But this only takes 1 numeric argument and moves it to the top. So, relevel

Reorder factor levels by day of the week in R

≡放荡痞女 提交于 2019-12-02 21:44:26
I have the following data.frame in R: > daily DoW Duration 1 Friday 14.0000000000000 2 Monday 21.0000000000000 3 Saturday 12.0000000000000 4 Thursday 28.0000000000000 5 Tuesday 12.0000000000000 6 Wednesday 91.0000000000000 7 Sunday 20.0000000000000 I'd like to change the order of the factor levels so that the weeks are in (US) day-of-week order. It looks like I can do this in a slow, puzzling way with relevel() . But this only takes 1 numeric argument and moves it to the top. So, relevel(daily$DoW, 7) , moves Sunday to the top, but the rest remain unordered (which means I need to relevel it in

Dropping factors which levels have observations smaller than a specific value-R

和自甴很熟 提交于 2019-12-02 18:32:56
问题 Let I have such data frame(df1) with factors: factor1 factor2 factor3 ------- ------- ------- d a x d a x b a x b c x b c y c c y c n y c n y c n y I want to drop factors from this data frame which one of elements have less than 3 observations. In this data frame factor1 has 3 levels(d,b and c). However d level has frequency 2. So I want to drop factor1 from this data frame. Resulted data frame should be as: factor2 factor3 ------- ------- a x a x a x c x c y c y n y n y n y How can I do this