Questions tagged [levels]

491 questions
3
votes
2 answers

Is there a function in R to change several similar factor levels at once?

My second question at stack overflow so all tips are welcome :) For clinical research I have to recode many dichotomous baseline characteristics that have several variations of "yes" and "no" in it. Currently i am recoding these variables one by one…
3
votes
2 answers

Factor levels by group

I have a data.table that looks as follows: library(data.table) dt <- fread( "Sex Height M 180 F 179 F 162 M 181 M 165 M 178 F 172 F 160", header = TRUE ) I would…
Tom
  • 2,173
  • 1
  • 17
  • 44
3
votes
1 answer

Select variables/columns in a dataframe by those with matching given factor levels

Possible to select variables in a dataframe by those matching certain factor levels, selecting columns based on their factor levels (used or unused)? I can summarise by levels or subset possibly by rows, but I wondered if columns could be selected…
23stacks1254
  • 369
  • 1
  • 9
3
votes
1 answer

How to fix "Factor level duplicated" Error in R heatmaply()?

I'm trying to make a heatmap based on that matrix: 1 2 3 4 5 6 7 C 6211 7608 8089 10514 7363 5375 7268 L 2459 2904 2573 3049 2221 1652 2311 N 3173 4213 3025 4324 2864 1524 2363 S 37 74 141 94 …
milva
  • 151
  • 1
  • 2
  • 9
3
votes
1 answer

How to create a factor with specified levels and labels, change the levels and adapt the labels step by step

I would like to do three things step by step and I am unfortunately stuck. Maybe someone could walk me through the process in R or point out my mistakes. # Create a dataset containing a factor with pre-defined levels and…
Dr. Fabian Habersack
  • 1,111
  • 12
  • 30
3
votes
0 answers

Can a factor levels be 0

I thought I perfectly understood the factor class in R, but finally, I may not. I can't find a way to transform a vector of 0/1 into a factor without transforming the original value in 1/2. Here is a minimal working…
3
votes
2 answers

copy factor level order from one column to another

I have two columns in a data.frame, that should have levels sorted in the same order, but I don't know how to do it in a straightforward manner. Here's the situation: library(ggplot2) library(dplyr) library(magrittr) set.seed(1) df1 <-…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
3
votes
1 answer

Cannot pipe variable to levels

I am working with a large data frame and rather than write manipulations to memory, I've been trying to do as much as a I with pipes. In trying to check my factor levels in intermediate steps, I ran into a problem using the levels function and…
JLC
  • 661
  • 7
  • 16
3
votes
2 answers

How can I keep NA when I change levels

I build a vector of factors containing NA. my_vec <- factor(c(NA,"a","b"),exclude=NULL) levels(my_vec) # [1] "a" "b" NA I change one of those levels. levels(my_vec)[levels(my_vec) == "b"] <- "c" NA disappears. levels(my_vec) # [1] "a" "c" How…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
3
votes
2 answers

r - convert factor to numeric and remove levels

I have a column in my data frame that is numbers 1, 2, 3, until 31. column is titled 'game' R tells me this column consists of Factors with 31 levels. How do I convert the factors to numbers without it displaying the levels so I can analyze it…
dudemcgregor
  • 69
  • 2
  • 2
  • 6
3
votes
5 answers

Get original association between levels and labels in factor variables

I'm looking for a function to get the original mapping table of factor variable. I import an Rdata file. I've got a factor variable named "FactVar". I know the mapping table for "FactVar" is as follows: "010025" -> city1 "015146" -> city2 "048017"…
Scido
  • 63
  • 5
3
votes
1 answer

Create a factor with more levels as actual used by default in R

I wrote a function in R to tabulate patient characteristics. If I have to tabulate a nominal variable it works fine, if there are no NAs for different categories. For example: I tabulate the NYHA Class at Baseline by the Studyarm. The NYHA Class…
3
votes
2 answers

how to substract numbers from levels

I would like to cut a vector of values ranging 0-70 to x number of categories, and would like the upper limit of each category. So far, I have tried this using cut() and am trying to extract the limits from levels. I have a list of levels, from…
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
3
votes
2 answers

unexpected behavior when extracting factor levels

Can someone explain why levels() shows three factor levels, while you can see that the vector has only two? > str(walk.df) 'data.frame': 10 obs. of 4 variables: $ walker : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2 > walk.df$walker …
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
3
votes
1 answer

predict.gam with new factor levels

I'm running a hurdle type analysis on species distribution data which involves two fitting steps. The first step is to model (m1) presence/absence data using all data with family=quasibinomial. The second step (m2) is to use positive presence only…
akbreezo
  • 115
  • 1
  • 1
  • 10
1 2
3
32 33