Paste multiple columns (strange behaviour)












3















I'm trying to paste several columns with the same name each into a new row but I get some strange behaviour. Example:



x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
x
> x
x y x
1 1 2 11
2 2 3 12
3 3 4 13
4 4 5 14
5 5 6 15
6 6 7 16
7 7 8 17
8 8 9 18
9 9 10 19
10 10 11 20
# now I try to paste columns to rows

> x2 <- data.frame(columns = unique(colnames(x)),
+ values = sapply(1:length(unique(colnames(x))), function(i)
+ paste(x[,(which(unique(colnames(x))[i]==colnames(x)))], collapse = ", ")))
> x2
columns values
1 x 1:10, 11:20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


What I wanted to have is just



> x2
columns values
1 x 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


Could somebody help me preventing this behaviour?










share|improve this question




















  • 1





    As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

    – alexis_laz
    Mar 6 '17 at 20:22













  • Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

    – JBGruber
    Mar 9 '17 at 8:20
















3















I'm trying to paste several columns with the same name each into a new row but I get some strange behaviour. Example:



x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
x
> x
x y x
1 1 2 11
2 2 3 12
3 3 4 13
4 4 5 14
5 5 6 15
6 6 7 16
7 7 8 17
8 8 9 18
9 9 10 19
10 10 11 20
# now I try to paste columns to rows

> x2 <- data.frame(columns = unique(colnames(x)),
+ values = sapply(1:length(unique(colnames(x))), function(i)
+ paste(x[,(which(unique(colnames(x))[i]==colnames(x)))], collapse = ", ")))
> x2
columns values
1 x 1:10, 11:20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


What I wanted to have is just



> x2
columns values
1 x 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


Could somebody help me preventing this behaviour?










share|improve this question




















  • 1





    As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

    – alexis_laz
    Mar 6 '17 at 20:22













  • Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

    – JBGruber
    Mar 9 '17 at 8:20














3












3








3








I'm trying to paste several columns with the same name each into a new row but I get some strange behaviour. Example:



x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
x
> x
x y x
1 1 2 11
2 2 3 12
3 3 4 13
4 4 5 14
5 5 6 15
6 6 7 16
7 7 8 17
8 8 9 18
9 9 10 19
10 10 11 20
# now I try to paste columns to rows

> x2 <- data.frame(columns = unique(colnames(x)),
+ values = sapply(1:length(unique(colnames(x))), function(i)
+ paste(x[,(which(unique(colnames(x))[i]==colnames(x)))], collapse = ", ")))
> x2
columns values
1 x 1:10, 11:20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


What I wanted to have is just



> x2
columns values
1 x 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


Could somebody help me preventing this behaviour?










share|improve this question
















I'm trying to paste several columns with the same name each into a new row but I get some strange behaviour. Example:



x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
x
> x
x y x
1 1 2 11
2 2 3 12
3 3 4 13
4 4 5 14
5 5 6 15
6 6 7 16
7 7 8 17
8 8 9 18
9 9 10 19
10 10 11 20
# now I try to paste columns to rows

> x2 <- data.frame(columns = unique(colnames(x)),
+ values = sapply(1:length(unique(colnames(x))), function(i)
+ paste(x[,(which(unique(colnames(x))[i]==colnames(x)))], collapse = ", ")))
> x2
columns values
1 x 1:10, 11:20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


What I wanted to have is just



> x2
columns values
1 x 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
2 y 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


Could somebody help me preventing this behaviour?







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:10









Cœur

17.6k9105145




17.6k9105145










asked Mar 6 '17 at 18:36









JBGruberJBGruber

1,580620




1,580620








  • 1





    As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

    – alexis_laz
    Mar 6 '17 at 20:22













  • Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

    – JBGruber
    Mar 9 '17 at 8:20














  • 1





    As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

    – alexis_laz
    Mar 6 '17 at 20:22













  • Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

    – JBGruber
    Mar 9 '17 at 8:20








1




1





As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

– alexis_laz
Mar 6 '17 at 20:22







As a note, there is no need to call unique(colnames(x)) so many times; you could save in a variable instead. Also, if you don't need this task for a specific formatting, you might consider avoiding coercing to "character" and pasteing -- aggregate(cbind(values = unlist(x, use.names = FALSE)) ~ rep(names(x), each = nrow(x)), FUN = c). Finally, you are passing a "list" of "integer"s (columns named "x") to paste which, implicitly, uses as.character on this "list" and the result is because of how as.character treats "list" arguments -- as.character(list(1:5))

– alexis_laz
Mar 6 '17 at 20:22















Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

– JBGruber
Mar 9 '17 at 8:20





Thanks that was a really helpful explanation! I use paste since my real data is actually text but it still makes sense.

– JBGruber
Mar 9 '17 at 8:20












1 Answer
1






active

oldest

votes


















0














x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
unique_cols <- unique(colnames(x))
x2 <- sapply(unique_cols, function(s) unlist(x[, unique_cols == s], use.names = F))
x2

$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$y
[1] 2 3 4 5 6 7 8 9 10 11


and



typeof(x2)
[1] "list"


in case you need a dataframe with the collapsed values:



df <- data.frame(columns = unique_cols,
value = as.character(sapply(x2, function(s) paste(s, collapse = ","), USE.NAMES = F)))

df

columns value
1 x 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2 y 2,3,4,5,6,7,8,9,10,11





share|improve this answer


























  • Worked like a charm :)

    – JBGruber
    Mar 9 '17 at 8:16











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42633084%2fpaste-multiple-columns-strange-behaviour%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









0














x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
unique_cols <- unique(colnames(x))
x2 <- sapply(unique_cols, function(s) unlist(x[, unique_cols == s], use.names = F))
x2

$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$y
[1] 2 3 4 5 6 7 8 9 10 11


and



typeof(x2)
[1] "list"


in case you need a dataframe with the collapsed values:



df <- data.frame(columns = unique_cols,
value = as.character(sapply(x2, function(s) paste(s, collapse = ","), USE.NAMES = F)))

df

columns value
1 x 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2 y 2,3,4,5,6,7,8,9,10,11





share|improve this answer


























  • Worked like a charm :)

    – JBGruber
    Mar 9 '17 at 8:16
















0














x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
unique_cols <- unique(colnames(x))
x2 <- sapply(unique_cols, function(s) unlist(x[, unique_cols == s], use.names = F))
x2

$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$y
[1] 2 3 4 5 6 7 8 9 10 11


and



typeof(x2)
[1] "list"


in case you need a dataframe with the collapsed values:



df <- data.frame(columns = unique_cols,
value = as.character(sapply(x2, function(s) paste(s, collapse = ","), USE.NAMES = F)))

df

columns value
1 x 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2 y 2,3,4,5,6,7,8,9,10,11





share|improve this answer


























  • Worked like a charm :)

    – JBGruber
    Mar 9 '17 at 8:16














0












0








0







x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
unique_cols <- unique(colnames(x))
x2 <- sapply(unique_cols, function(s) unlist(x[, unique_cols == s], use.names = F))
x2

$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$y
[1] 2 3 4 5 6 7 8 9 10 11


and



typeof(x2)
[1] "list"


in case you need a dataframe with the collapsed values:



df <- data.frame(columns = unique_cols,
value = as.character(sapply(x2, function(s) paste(s, collapse = ","), USE.NAMES = F)))

df

columns value
1 x 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2 y 2,3,4,5,6,7,8,9,10,11





share|improve this answer















x <- data.frame(x = 1:10, y= 2:11, z= 11:20)
colnames(x) <- c("x", "y", "x")
unique_cols <- unique(colnames(x))
x2 <- sapply(unique_cols, function(s) unlist(x[, unique_cols == s], use.names = F))
x2

$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$y
[1] 2 3 4 5 6 7 8 9 10 11


and



typeof(x2)
[1] "list"


in case you need a dataframe with the collapsed values:



df <- data.frame(columns = unique_cols,
value = as.character(sapply(x2, function(s) paste(s, collapse = ","), USE.NAMES = F)))

df

columns value
1 x 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2 y 2,3,4,5,6,7,8,9,10,11






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 '17 at 19:10

























answered Mar 6 '17 at 18:55









CodutieCodutie

806414




806414













  • Worked like a charm :)

    – JBGruber
    Mar 9 '17 at 8:16



















  • Worked like a charm :)

    – JBGruber
    Mar 9 '17 at 8:16

















Worked like a charm :)

– JBGruber
Mar 9 '17 at 8:16





Worked like a charm :)

– JBGruber
Mar 9 '17 at 8:16


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42633084%2fpaste-multiple-columns-strange-behaviour%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values