####### Reprex 3rd November 2022 #########
install.packages(c("httr", "jsonlite", "openssl","wkb","tidyverse","dplyr","progress"))
library(httr)
library(jsonlite)
library(openssl)
library(wkb)
library(tidyverse)
library(dplyr)
library(progress)
Making dataframe
a = c("apple", "pear", "banana", "apple", "pear", "banana")
b = c(10, 19, 50, 1, 9, 5)
c = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE)
my_df <- data.frame(a,b,c)
Nesting dataframe
my_df %>%
nest_by(a) %>%
assign("my_df",. , inherits= TRUE)
I can subset a particular row .....
apples <- my_df[1, ]
But, I need to subset using specific row names that are not consecutive.
Names <- c("apple", "pear")
Both lines of code produce a blank dataframes. Why is this? Can you help?
apples_pears <- my_df[c("apple", "pear"), ]
apples_pears <- my_df[c(Names),]
End of reprex
FJCC
2
The values in column a
are not row names. You have to refer to column a
to get the subset you want. Here are two methods to do that:
library(tidyverse)
a = c("apple", "pear", "banana", "apple", "pear", "banana")
b = c(10, 19, 50, 1, 9, 5)
c = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE)
my_df <- data.frame(a,b,c)
my_df %>%
nest_by(a) %>%
assign("my_df",. , inherits= TRUE)
Names <- c("apple", "pear")
row.names(my_df)
#> [1] "1" "2" "3"
apples_pears <- my_df[my_df$a %in% Names,]
apples_pears
#> # A tibble: 2 × 2
#> # Rowwise: a
#> a data
#> <chr> <list<tibble[,2]>>
#> 1 apple [2 × 2]
#> 2 pear [2 × 2]
#Also, filter is a function in dplyr
filter(my_df, a %in% Names)
#> # A tibble: 2 × 2
#> # Rowwise: a
#> a data
#> <chr> <list<tibble[,2]>>
#> 1 apple [2 × 2]
#> 2 pear [2 × 2]
Created on 2022-11-02 with reprex v2.0.2
1 Like
Thanks very much for the two solutions. Also, many thanks to taking the time to read and respond. It was much appreciated.
system
Closed
4
This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.