Round dbl to nearlest integer and convert to ordered factor in R
up vote
0
down vote
favorite
I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.
# example data
tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -29L))
# round to the nearest integer
scores <- round(tmp, digits = 0)
# convert to an ordered factor
factors <-
ordered(
scores,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
This code returns the following:
variable1
<NA>
Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
I thought it was because of the NA
in my dataset, but I still get the same thing after omitting NA.
What am I doing wrong?
r
add a comment |
up vote
0
down vote
favorite
I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.
# example data
tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -29L))
# round to the nearest integer
scores <- round(tmp, digits = 0)
# convert to an ordered factor
factors <-
ordered(
scores,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
This code returns the following:
variable1
<NA>
Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
I thought it was because of the NA
in my dataset, but I still get the same thing after omitting NA.
What am I doing wrong?
r
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.
# example data
tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -29L))
# round to the nearest integer
scores <- round(tmp, digits = 0)
# convert to an ordered factor
factors <-
ordered(
scores,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
This code returns the following:
variable1
<NA>
Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
I thought it was because of the NA
in my dataset, but I still get the same thing after omitting NA.
What am I doing wrong?
r
I have a numeric column within a dataframe that I want to round to the nearest integer and then convert into an ordered factor. This seems fairly straight forward, but I can't figure out why this isn't working as expected.
# example data
tmp <- structure(list(variable1 = c(X1 = 3.66666666666667, X2 = 2.66666666666667,
X3 = 3.33333333333333, X4 = 3, X5 = 2, X6 = 2, X7 = NA, X8 = 3.33333333333333,
X9 = 2.66666666666667, X10 = 4, X11 = 3.66666666666667, X12 = 3,
X13 = 3.66666666666667, X14 = 3.33333333333333, X15 = 3.33333333333333,
X16 = 4, X17 = 3.33333333333333, X18 = 3, X19 = 3, X20 = 3, X21 = 3,
X22 = 3.33333333333333, X23 = 3.33333333333333, X24 = 2.66666666666667,
X25 = 2, X26 = 3.33333333333333, X27 = 3, X28 = 3, X29 = 2.66666666666667)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -29L))
# round to the nearest integer
scores <- round(tmp, digits = 0)
# convert to an ordered factor
factors <-
ordered(
scores,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
This code returns the following:
variable1
<NA>
Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
I thought it was because of the NA
in my dataset, but I still get the same thing after omitting NA.
What am I doing wrong?
r
r
asked Nov 11 at 3:53
CurtLH
6921032
6921032
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It is because you are passing a data frame and not a vector to ordered()
:
str(scores)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
# $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...
But ordered()
needs a vector:
ordered(x, ...)
x: a vector of data, usually taking a small number of distinct
values.
Use scores$variable1
instead:
factors <-
ordered(
scores$variable1,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
factors
# [1] Strongly Agree Agree Agree Agree Disagree
# [6] Disagree <NA> Agree Agree Strongly Agree
# [11] Strongly Agree Agree Strongly Agree Agree Agree
# [16] Strongly Agree Agree Agree Agree Agree
# [21] Agree Agree Agree Agree Disagree
# [26] Agree Agree Agree Agree
# Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It is because you are passing a data frame and not a vector to ordered()
:
str(scores)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
# $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...
But ordered()
needs a vector:
ordered(x, ...)
x: a vector of data, usually taking a small number of distinct
values.
Use scores$variable1
instead:
factors <-
ordered(
scores$variable1,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
factors
# [1] Strongly Agree Agree Agree Agree Disagree
# [6] Disagree <NA> Agree Agree Strongly Agree
# [11] Strongly Agree Agree Strongly Agree Agree Agree
# [16] Strongly Agree Agree Agree Agree Agree
# [21] Agree Agree Agree Agree Disagree
# [26] Agree Agree Agree Agree
# Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
add a comment |
up vote
1
down vote
accepted
It is because you are passing a data frame and not a vector to ordered()
:
str(scores)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
# $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...
But ordered()
needs a vector:
ordered(x, ...)
x: a vector of data, usually taking a small number of distinct
values.
Use scores$variable1
instead:
factors <-
ordered(
scores$variable1,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
factors
# [1] Strongly Agree Agree Agree Agree Disagree
# [6] Disagree <NA> Agree Agree Strongly Agree
# [11] Strongly Agree Agree Strongly Agree Agree Agree
# [16] Strongly Agree Agree Agree Agree Agree
# [21] Agree Agree Agree Agree Disagree
# [26] Agree Agree Agree Agree
# Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It is because you are passing a data frame and not a vector to ordered()
:
str(scores)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
# $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...
But ordered()
needs a vector:
ordered(x, ...)
x: a vector of data, usually taking a small number of distinct
values.
Use scores$variable1
instead:
factors <-
ordered(
scores$variable1,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
factors
# [1] Strongly Agree Agree Agree Agree Disagree
# [6] Disagree <NA> Agree Agree Strongly Agree
# [11] Strongly Agree Agree Strongly Agree Agree Agree
# [16] Strongly Agree Agree Agree Agree Agree
# [21] Agree Agree Agree Agree Disagree
# [26] Agree Agree Agree Agree
# Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
It is because you are passing a data frame and not a vector to ordered()
:
str(scores)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 29 obs. of 1 variable:
# $ variable1: num 4 3 3 3 2 2 NA 3 3 4 ...
But ordered()
needs a vector:
ordered(x, ...)
x: a vector of data, usually taking a small number of distinct
values.
Use scores$variable1
instead:
factors <-
ordered(
scores$variable1,
levels = c(1, 2, 3, 4),
labels = c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
)
factors
# [1] Strongly Agree Agree Agree Agree Disagree
# [6] Disagree <NA> Agree Agree Strongly Agree
# [11] Strongly Agree Agree Strongly Agree Agree Agree
# [16] Strongly Agree Agree Agree Agree Agree
# [21] Agree Agree Agree Agree Disagree
# [26] Agree Agree Agree Agree
# Levels: Strongly Disagree < Disagree < Agree < Strongly Agree
answered Nov 11 at 4:13
prosoitos
822219
822219
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245699%2fround-dbl-to-nearlest-integer-and-convert-to-ordered-factor-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown