Questions tagged [levels]
The levels tag has no usage guidance.
levels
494
questions
154
votes
9
answers
107k
views
Reorder levels of a factor without changing order of values
I have data frame with some numerical variables and some categorical factor variables. The order of levels for those factors is not the way I want them to be.
numbers <- 1:4
letters <- factor(...
129
votes
4
answers
55k
views
Keep unused levels in bar plot
I want to plot unused levels (that is, levels where the count is 0) in my bar-plot, however, unused levels are dropped and I cannot figure out how to keep them
df <- data.frame(type=c("A", "A", "A"...
122
votes
4
answers
8k
views
`levels<-`( What sorcery is this?
In an answer to another question, @Marek posted the following solution:
https://stackoverflow.com/a/10432263/636656
dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L,
...
94
votes
1
answer
202k
views
Re-ordering factor levels in data frame [duplicate]
I have a data.frame as shown below:
task measure
right m1
left m2
up m3
down m4
front m5
back m6
.
.
.
The task column takes only six different values, which are treated as ...
51
votes
4
answers
83k
views
How can I drop unused levels from a data frame?
Given the following mock data:
set.seed(123)
x <- data.frame(let = sample(letters[1:5], 100, replace = T),
num = sample(1:10, 100, replace = T))
y <- subset(x, let != 'a')
...
38
votes
2
answers
2k
views
Why is the terminology of labels and levels in factors so weird?
An example of a non-settable function would be labels. You can only set factor labels when they are created with the factor() function. There is no labels<- function. Not that 'labels' and 'levels' ...
35
votes
14
answers
38k
views
Counting depth or the deepest level a nested list goes to
A have a real problem (and a headache) with an assignment...
I'm in an introductory programming class, and I have to write a function that, given a list, will return the "maximum" depth it goes to...
...
25
votes
5
answers
104k
views
Mean by factor by level
Maybe this is simple but I can't find answer on web. I have problem with mean calculation by factors by level.
My data looks typicaly:
factor, value
a,1
a,2
b,1
b,1
b,1
c,1
I want to get vector A ...
24
votes
2
answers
102k
views
R- split histogram according to factor level
This is my data:
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
If I want to plot historgram of diff, I do this:
hist(data$diff)
But what I want to do to split my ...
20
votes
4
answers
70k
views
How to change name of factor levels?
training set
trainSample <- cbind(data[1:980,1], data[1:980,2]) cl <-
factor(c(data[1:980,3]))
test set
testSample <- data(data[981:1485,1], data[981:1485,2])
cl.test <- clknn
...
17
votes
7
answers
38k
views
Remove first levels of identifier in array [duplicate]
I think this has been up before, but could'nt find any answer to it. If it's already answered please point me in the right direction with a link.
I have an array that I wan't to remove the first ...
15
votes
3
answers
37k
views
What are levels in R?
I understand this is a very basic question but I don't understand what levels mean in R.
For reference, I have done a simple script to read CSV table, filter on one of the fields, pass this on to a ...
12
votes
2
answers
11k
views
Removing the levels attribute in the output - R
I am new to R Programming. I wrote a sample program and that returns a value of a particular column in a matrix. When I print the value i get something like this
[1] APPLE
2 Levels : 1 2
How do I ...
11
votes
4
answers
13k
views
Recode/relevel data.frame factors with different levels
Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it's quite straightforward. There's a similar recode function in car package, and it does ...
9
votes
0
answers
4k
views
removing an explicit mandatory label using icacls
I was doing some experimentation with integrity levels and icacls on Windows 7. I set mandatory integrity levels, so that I get something that looks like this:
C:\Debug>icacls test.exe
test.exe ...
8
votes
3
answers
45k
views
Levels function returning NULL
I'm hoping this is an easy fix. Whenever I run levels(df) am I given a NULL output. This isn't specific to my data frame as it occurs with any set of data that I use. I am thinking that there may ...
8
votes
1
answer
4k
views
ImageMagick - How do I flatten white levels to pure white?
I have a png image with a white background which I'd like to turn transparent. This is fairly simple with this command:
$ convert image.png -transparent white image-trans.png
However, if the white ...
7
votes
4
answers
65k
views
Levels in R Dataframe
I imported data from a .csv file, and attached the dataset.
My problem: one variable is in integer form and has 295 levels. I need to use this variable to create others, but I don't know how to deal ...
6
votes
2
answers
23k
views
Removing Specific factor level from factor variable
I have a data frame that has several variables that have 5 factor levels. I want to delete only one of those levels. First I assigned all instances of of that level to NA, and then used the droplevels ...
4
votes
2
answers
21k
views
Change reference level for variable in R
I have a data set, (call it DATA) with a variable, COLOR. The mode of COLOR is numeric and the class is factor. First, I'm a bit confused by the "numeric" -- when printed out, the data for COLOR are ...
4
votes
3
answers
2k
views
The paste() function and ordering levels of variables
I have two columns in the following data frame, and each column has levels according to a specific order:
head(x1)
soa congruency
1 200 9
2 102 2
3 68 1
4 68 9
...
4
votes
2
answers
909
views
Best practices for implementing levels in cocos2d games
I'm making a simple cocos2d adventure game, but have no clue how to implement any sort of levels. I've searched for tutorials, but can't find any.
Is there anything I can use to figure out levels in ...
4
votes
1
answer
2k
views
Getting the keys out of a Multiindex?
I've got a MultiIndex with IDs and Dates, of the form:
MultiIndex(levels=[[196003, 196005, 196007, 196009, 196012, 196103, 196105, 196107, 196109, 196112, 196203, 196205, 196207, 196209, 196212, ...
4
votes
1
answer
2k
views
How to swap the column header in pandas pivot table?
Following up from this question
I got a datframe after pivoting like this .
AVG GrossProfit AVG PMV Loss% Sales
ParentAuction Copart IAA ...
4
votes
1
answer
242
views
R: grouping factors everytime n unique levels occur in data
I want a new grouping every time there's 5 unique levels
Ex. varB is the desired result
structure(list(varA = structure(c(2L, 2L, 3L, 5L, 4L, 1L, 1L,
2L, 3L, 5L, 4L, 4L, 1L), .Label = c("badger", "...
4
votes
4
answers
3k
views
Highcharts - Hide child labels in a multiple levels and multiple layouts treemap
On highcharts, I have a treemap with 2 levels, each with a different layout algorithm. Now I want to limit what we can see to the current level. It means that on level 1, I don't want to see the ...
4
votes
1
answer
323
views
JQuery Sortable strange behavior connectWith div
I'm trying to connect multiple boxes in multiple levels and sort them between the levels using JQuery Sortable. The problem is that my current solution have a strange behavior when I try to drop items ...
3
votes
2
answers
33k
views
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 ...
3
votes
3
answers
8k
views
remove level from a list extract to a data fram
I have a data frame like this
df <- data.frame(letters=letters[1:5], numbers=seq(1:5))
and lets say that I want to extram the first column into a list
firstColumn <- df[,1]
> ...
3
votes
2
answers
2k
views
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 "...
3
votes
2
answers
2k
views
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)
...
3
votes
1
answer
1k
views
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 ...
3
votes
2
answers
3k
views
Get the number of levels of a categorical variable as a single number in Stata
I am trying to find a way to get the number of levels of a categorical variable as a single number. For example if I have a variable X with 4 levels I need to somehow get that number. If I type ...
3
votes
2
answers
5k
views
How can I sort a dataframe by a predetermined order of factor levels in R?
I have a data frame in which one column consists of unique factors. I want to sort this data frame by a predefined order of factor levels, independend of the original order.
For example my data ...
3
votes
2
answers
199
views
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 ...
3
votes
2
answers
1k
views
How can I reclassify a categorical raster in terra using category names
I'd like to be able to merge two categories in a categorical raster. The only solution I've figured out so far uses the level index number, not the name of the category. How could I do this using the ...
3
votes
1
answer
751
views
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 ...
3
votes
1
answer
1k
views
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 ...
3
votes
6
answers
1k
views
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 ...
3
votes
2
answers
242
views
How to perform conditional mutate on unspecified columns (e.g. matching regex)?
I have a nested data.frame - df_nested, there one of column contains df:
df <- tibble(ID_Value = 1:8,
xyz001 = c("text4", NA, NA, NA, NA, NA, NA, "text2"),
...
3
votes
2
answers
450
views
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 &...
3
votes
2
answers
411
views
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
...
3
votes
1
answer
3k
views
How can I select text from a row and column of a data frame without returning all Levels in R? [duplicate]
Possible Duplicate:
dropping factor levels in a subsetted data frame in R
I would like to get the text from a cell in a data frame (and use that text to create a file). However, whenever I select ...
3
votes
3
answers
4k
views
Error with levels using mlogit in R
I am having some trouble with levels... Running the following:
library(mlogit)
panel.datasm = data.frame(
cbind(
round(runif(100, min=1, max=6)),
rep(1:20,each=5), runif(100,...
3
votes
2
answers
523
views
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"] <- ...
3
votes
1
answer
36
views
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 ...
3
votes
1
answer
555
views
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 ...
3
votes
0
answers
3k
views
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 ...
3
votes
1
answer
2k
views
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 ...
2
votes
4
answers
7k
views
How to Check Which level category it is for wordpress?
Let me tell you the scenario first say the structure of the categories in wordpress is like this
Level 1: Top
Level 2: -Nextme_1
Level 3: --Nextme_2
--Nextme_3
Level 4: ---Nextme_4
-...