Questions tagged [levels]
491 questions
152
votes
9 answers
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(c("a", "b", "c", "d"))
df <- data.frame(numbers,…

crangos
- 1,523
- 2
- 10
- 4
126
votes
4 answers
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", "B", "B"), group=rep("group1", 5))
df$type <-…

Ulrik
- 1,575
- 2
- 10
- 10
120
votes
4 answers
`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,
7L, 11L, 5L, 11L, 4L, 3L,…

Ari B. Friedman
- 71,271
- 35
- 175
- 235
94
votes
1 answer
Re-ordering factor levels in data frame
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 factors, and are ordered by R as: "back", "down",…

siva82kb
- 1,910
- 4
- 19
- 28
51
votes
4 answers
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')
Creating a table of y$let yields
a b c d e
0 20 21 22…

Waldir Leoncio
- 10,853
- 19
- 77
- 107
38
votes
2 answers
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' in factors make any sense....
> fac <- factor(1:3,…

IRTFM
- 258,963
- 21
- 364
- 487
35
votes
14 answers
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...
For example: [1,2,3] will return 1, [1,[2,3]]…

dhcarmona
- 402
- 2
- 10
- 29
25
votes
5 answers
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 contains mean only for level "a"
If I type A on…

Bartek Taciak
- 295
- 1
- 3
- 6
23
votes
2 answers
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 histogram according to type. I could do…

89_Simple
- 3,393
- 3
- 39
- 94
20
votes
4 answers
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
prediction
k <- knn(trainSample, testSample, cl, k = 5)
output
< k
…

Beginner questions
- 343
- 1
- 4
- 15
16
votes
7 answers
Remove first levels of identifier in array
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 levels of identifier. I think there is a function for…

Fredrik
- 627
- 6
- 14
- 28
15
votes
3 answers
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 new variable and clear the memory allocated for the…

ApplePie
- 8,814
- 5
- 39
- 60
12
votes
2 answers
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 get only the value without the levels in the…

Minions
- 1,273
- 1
- 11
- 28
11
votes
4 answers
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 the trick, but let's presuppose that I want to…

aL3xa
- 35,415
- 18
- 79
- 112
9
votes
0 answers
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 Everyone:(I)(RX)
…

chut7
- 103
- 1
- 4