Re: [R] How to obtain named vector from single-column data frame?
Dear David and Rui, Thanks a lot to both of you for your very fast answers; problem solved! :) Regards, Guido From: David Carlson Sent: 06 May 2022 20:26 To: Hooiveld, Guido Cc: r-help@r-project.org Subject: Re: [R] How to obtain named vector from single-column data frame? Just use names(unlist(df[, "VarY", drop=FALSE])) # [1] "VarY1" "VarY2" "VarY3" "VarY4" "VarY5" When you extract a single column from a data frame it converts it to a vector by default. David L Carlson On Fri, May 6, 2022 at 1:05 PM Hooiveld, Guido mailto:guido.hooiv...@wur.nl>> wrote: Dear all, I wrote a some code in which I 'convert' a data frame to a named vector using the function unlist(). This works very nicely for my use case, because each entry of the resulting vector is named after the column name of the data frame, ZjQcmQRYFpfptBannerStart This Message Is From an External Sender This message came from outside your organization. ZjQcmQRYFpfptBannerEnd Dear all, I wrote a some code in which I 'convert' a data frame to a named vector using the function unlist(). This works very nicely for my use case, because each entry of the resulting vector is named after the column name of the data frame, with a number automatically appended. I am using these names for the subsequent part of my code. However, I noticed that when I subset the data frame so it contains only a single column, the naming of the vector (as described above) doesn't occur anymore (i.e. names() = NULL). This breaks my downstream code. Any suggestion on how to still obtain a named vector from such single-column data frame? Thanks, Guido > df <- data.frame("VarX" = c("A",2,"D",2,1) , + "VarY" = c(5,7,9,8,7) ) > > unlist(df) #nice! VarX1 VarX2 VarX3 VarX4 VarX5 VarY1 VarY2 VarY3 VarY4 VarY5 "A" "2" "D" "2" "1" "5" "7" "9" "8" "7" > names(unlist(df)) [1] "VarX1" "VarX2" "VarX3" "VarX4" "VarX5" "VarY1" "VarY2" "VarY3" "VarY4" [10] "VarY5" > > > > unlist(df[, "VarY"]) #where are the names now? Expected them to be "VarY1" > ... "VarY5" [1] 5 7 9 8 7 > names(unlist(df[, "VarY"])) NULL > __ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXEBOEqSIog$<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help__%3B!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXEBOEqSIog%24&data=05%7C01%7Cguido.hooiveld%40wur.nl%7Cbdd102be7b9042ccfec508da2f8df4c0%7C27d137e5761f4dc1af88d26430abb18f%7C0%7C0%7C637874584008711589%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=UGXlmkwuoBkGb%2F7dLZ0%2F3iFXJ9rtabtRB8%2FZ1sbMe8g%3D&reserved=0> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXECRkw_-4w$<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html__%3B!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXECRkw_-4w%24&data=05%7C01%7Cguido.hooiveld%40wur.nl%7Cbdd102be7b9042ccfec508da2f8df4c0%7C27d137e5761f4dc1af88d26430abb18f%7C0%7C0%7C637874584008711589%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=okqIQIFy6e1vHNTqezf8cDO2tmt%2BuZnqCe98CUnGknQ%3D&reserved=0> and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to obtain named vector from single-column data frame?
Just use names(unlist(df[, "VarY", drop=FALSE])) # [1] "VarY1" "VarY2" "VarY3" "VarY4" "VarY5" When you extract a single column from a data frame it converts it to a vector by default. David L Carlson On Fri, May 6, 2022 at 1:05 PM Hooiveld, Guido wrote: > Dear all, I wrote a some code in which I 'convert' a data frame to a named > vector using the function unlist(). This works very nicely for my use case, > because each entry of the resulting vector is named after the column name > of the data frame, > ZjQcmQRYFpfptBannerStart > This Message Is From an External Sender > This message came from outside your organization. > > ZjQcmQRYFpfptBannerEnd > > Dear all, > > I wrote a some code in which I 'convert' a data frame to a named vector using > the function unlist(). This works very nicely for my use case, because each > entry of the resulting vector is named after the column name of the data > frame, with a number automatically appended. I am using these names for the > subsequent part of my code. > > However, I noticed that when I subset the data frame so it contains only a > single column, the naming of the vector (as described above) doesn't occur > anymore (i.e. names() = NULL). This breaks my downstream code. Any suggestion > on how to still obtain a named vector from such single-column data frame? > > Thanks, > Guido > > > > df <- data.frame("VarX" = c("A",2,"D",2,1) , > + "VarY" = c(5,7,9,8,7) ) > > > > unlist(df) #nice! > VarX1 VarX2 VarX3 VarX4 VarX5 VarY1 VarY2 VarY3 VarY4 VarY5 > "A" "2" "D" "2" "1" "5" "7" "9" "8" "7" > > names(unlist(df)) > [1] "VarX1" "VarX2" "VarX3" "VarX4" "VarX5" "VarY1" "VarY2" "VarY3" "VarY4" > [10] "VarY5" > > > > > > > > unlist(df[, "VarY"]) #where are the names now? Expected them to be "VarY1" > > ... "VarY5" > [1] 5 7 9 8 7 > > names(unlist(df[, "VarY"])) > NULL > > > > __r-h...@r-project.org mailing > list -- To UNSUBSCRIBE and more, > seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXEBOEqSIog$ > PLEASE do read the posting guide > https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!EUndg3P-mraBuPMXbhvgZkIok13frVGjcD2ebHWlTHVrETkmNqEfYu0YPT6BEisdkhcijOfxsGlfsGoPXECRkw_-4w$ > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to obtain named vector from single-column data frame?
Hello, When you unlist, the dimension is dropped since the result is just one vector. Add drop = FALSE and that's it. unlist(df[, "VarY", drop = FALSE]) Hope this helps, Rui Barradas Às 19:05 de 06/05/2022, Hooiveld, Guido escreveu: Dear all, I wrote a some code in which I 'convert' a data frame to a named vector using the function unlist(). This works very nicely for my use case, because each entry of the resulting vector is named after the column name of the data frame, with a number automatically appended. I am using these names for the subsequent part of my code. However, I noticed that when I subset the data frame so it contains only a single column, the naming of the vector (as described above) doesn't occur anymore (i.e. names() = NULL). This breaks my downstream code. Any suggestion on how to still obtain a named vector from such single-column data frame? Thanks, Guido df <- data.frame("VarX" = c("A",2,"D",2,1) , + "VarY" = c(5,7,9,8,7) ) unlist(df) #nice! VarX1 VarX2 VarX3 VarX4 VarX5 VarY1 VarY2 VarY3 VarY4 VarY5 "A" "2" "D" "2" "1" "5" "7" "9" "8" "7" names(unlist(df)) [1] "VarX1" "VarX2" "VarX3" "VarX4" "VarX5" "VarY1" "VarY2" "VarY3" "VarY4" [10] "VarY5" unlist(df[, "VarY"]) #where are the names now? Expected them to be "VarY1" ... "VarY5" [1] 5 7 9 8 7 names(unlist(df[, "VarY"])) NULL __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.