Cbind result of aplucter to dataframe in R
I perform cluster analysis and i must use apcluster library
library(apcluster)
## create four synthetic 2D clusters
cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
x <- rbind(cl1, cl2, cl3, cl4)
## run apcluster() (you may replace the Euclidean distance by a different
## distance, e.g. driving distance, driving time)
apres <- apcluster(negDistMat(r=2), x, q=0)
After the clustering, selected cluster i want to join to x data.frame
to see
observation and what it cluster.
I did so
cbind(x, mycluster=apres$Clusters)
and error
Error in apres$Clusters : $ operator not defined for this S4 class
How should i do?
[,1] [,2] cluster
[1,] 0.3671142 0.6204475 2
r dataframe
add a comment |
I perform cluster analysis and i must use apcluster library
library(apcluster)
## create four synthetic 2D clusters
cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
x <- rbind(cl1, cl2, cl3, cl4)
## run apcluster() (you may replace the Euclidean distance by a different
## distance, e.g. driving distance, driving time)
apres <- apcluster(negDistMat(r=2), x, q=0)
After the clustering, selected cluster i want to join to x data.frame
to see
observation and what it cluster.
I did so
cbind(x, mycluster=apres$Clusters)
and error
Error in apres$Clusters : $ operator not defined for this S4 class
How should i do?
[,1] [,2] cluster
[1,] 0.3671142 0.6204475 2
r dataframe
add a comment |
I perform cluster analysis and i must use apcluster library
library(apcluster)
## create four synthetic 2D clusters
cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
x <- rbind(cl1, cl2, cl3, cl4)
## run apcluster() (you may replace the Euclidean distance by a different
## distance, e.g. driving distance, driving time)
apres <- apcluster(negDistMat(r=2), x, q=0)
After the clustering, selected cluster i want to join to x data.frame
to see
observation and what it cluster.
I did so
cbind(x, mycluster=apres$Clusters)
and error
Error in apres$Clusters : $ operator not defined for this S4 class
How should i do?
[,1] [,2] cluster
[1,] 0.3671142 0.6204475 2
r dataframe
I perform cluster analysis and i must use apcluster library
library(apcluster)
## create four synthetic 2D clusters
cl1 <- cbind(rnorm(30, 0.3, 0.05), rnorm(30, 0.7, 0.04))
cl2 <- cbind(rnorm(30, 0.7, 0.04), rnorm(30, 0.4, .05))
cl3 <- cbind(rnorm(20, 0.50, 0.03), rnorm(20, 0.72, 0.03))
cl4 <- cbind(rnorm(25, 0.50, 0.03), rnorm(25, 0.42, 0.04))
x <- rbind(cl1, cl2, cl3, cl4)
## run apcluster() (you may replace the Euclidean distance by a different
## distance, e.g. driving distance, driving time)
apres <- apcluster(negDistMat(r=2), x, q=0)
After the clustering, selected cluster i want to join to x data.frame
to see
observation and what it cluster.
I did so
cbind(x, mycluster=apres$Clusters)
and error
Error in apres$Clusters : $ operator not defined for this S4 class
How should i do?
[,1] [,2] cluster
[1,] 0.3671142 0.6204475 2
r dataframe
r dataframe
asked Nov 17 '18 at 10:03
d-maxd-max
728
728
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The first issue is that apres
isn't an S3 class object and using $
isn't going to work. See apres
and you will see @
used in a similar fashion. In particular, as to get the resulting clusters, you want
cls <- apres@clusters
Next thing is that cls
is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be
clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
head(clsNm)
# 1 1 1 1 1 1
# 1 2 3 4 5 6
Then we may do what you tried
head(cbind(x, cluster = as.integer(names(clsNm))), 2)
# cluster
# [1,] 0.2526693 0.6764543 1
# [2,] 0.2781545 0.6774726 1
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53350167%2fcbind-result-of-aplucter-to-dataframe-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first issue is that apres
isn't an S3 class object and using $
isn't going to work. See apres
and you will see @
used in a similar fashion. In particular, as to get the resulting clusters, you want
cls <- apres@clusters
Next thing is that cls
is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be
clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
head(clsNm)
# 1 1 1 1 1 1
# 1 2 3 4 5 6
Then we may do what you tried
head(cbind(x, cluster = as.integer(names(clsNm))), 2)
# cluster
# [1,] 0.2526693 0.6764543 1
# [2,] 0.2781545 0.6774726 1
add a comment |
The first issue is that apres
isn't an S3 class object and using $
isn't going to work. See apres
and you will see @
used in a similar fashion. In particular, as to get the resulting clusters, you want
cls <- apres@clusters
Next thing is that cls
is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be
clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
head(clsNm)
# 1 1 1 1 1 1
# 1 2 3 4 5 6
Then we may do what you tried
head(cbind(x, cluster = as.integer(names(clsNm))), 2)
# cluster
# [1,] 0.2526693 0.6764543 1
# [2,] 0.2781545 0.6774726 1
add a comment |
The first issue is that apres
isn't an S3 class object and using $
isn't going to work. See apres
and you will see @
used in a similar fashion. In particular, as to get the resulting clusters, you want
cls <- apres@clusters
Next thing is that cls
is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be
clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
head(clsNm)
# 1 1 1 1 1 1
# 1 2 3 4 5 6
Then we may do what you tried
head(cbind(x, cluster = as.integer(names(clsNm))), 2)
# cluster
# [1,] 0.2526693 0.6764543 1
# [2,] 0.2781545 0.6774726 1
The first issue is that apres
isn't an S3 class object and using $
isn't going to work. See apres
and you will see @
used in a similar fashion. In particular, as to get the resulting clusters, you want
cls <- apres@clusters
Next thing is that cls
is a list of four elements, each elements containing indices of observations belonging to the corresponding cluster. One way then to associate the observation (value) with its cluster (name) would be
clsNm <- sort(setNames(unlist(cls), rep(1:length(cls), times = lengths(cls))))
head(clsNm)
# 1 1 1 1 1 1
# 1 2 3 4 5 6
Then we may do what you tried
head(cbind(x, cluster = as.integer(names(clsNm))), 2)
# cluster
# [1,] 0.2526693 0.6764543 1
# [2,] 0.2781545 0.6774726 1
answered Nov 17 '18 at 10:14
Julius VainoraJulius Vainora
34.7k76079
34.7k76079
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53350167%2fcbind-result-of-aplucter-to-dataframe-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