Hello:
I'm wanting to create new datasets using only select rows.
I have a survey with row responses of "Bell", "Rogers" and "Telus"
I would like to be able to create individual datasets for each one of these row responses.
I am familiar with selecting variables in columns but not with rows.
What is the code for creating a new dataset with isolated rows? Thanks for helping out
Hi!
To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
leungi
September 22, 2019, 8:01pm
3
What is the goal here?
As an example, if I'm trying to count how many response by category:
library(tidyverse)
set.seed(201922)
df <- tibble(id = 1:10,
resp = sample(letters[1:3], 10, replace = TRUE))
df
#> # A tibble: 10 x 2
#> id resp
#> <int> <chr>
#> 1 1 a
#> 2 2 c
#> 3 3 b
#> 4 4 b
#> 5 5 c
#> 6 6 c
#> 7 7 c
#> 8 8 b
#> 9 9 c
#> 10 10 b
df %>%
count(resp, name = "count")
#> # A tibble: 3 x 2
#> resp count
#> <chr> <int>
#> 1 a 1
#> 2 b 4
#> 3 c 5
Created on 2019-09-22 by the reprex package (v0.3.0)
#> Carrier Sepal.Width Petal.Length Petal.Width Species
#> 1 Bell 3.5 1.4 0.2 setosa
#> 2 Telus 3.0 1.4 0.2 setosa
#> 3 Rogers 3.2 1.3 0.2 setosa
#> 4 Rogers 3.1 1.5 0.2 setosa
#> 5 Telus 3.6 1.4 0.2 setosa
#> 6 Bell 3.9 1.7 0.4 setosa
How would I create a new dataset only containing responses from Bell?
Hello! Thank you for your help. The goal is to determine behaviours from Bell Consumers in isolation. I have replied to my original post providing an example. I want to create a new dataset to work with. That contains only responses of respondents who are with Bell.
Does that help?
I'm assuming you prefer base R solutions, this would be how to do it
df <- data.frame(stringsAsFactors=FALSE,
Carrier = c("Bell", "Telus", "Rogers", "Rogers", "Telus", "Bell"),
Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7),
Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4),
Species = c("setosa", "setosa", "setosa", "setosa", "setosa", "setosa")
)
df[df$Carrier == "Bell",]
#> Carrier Sepal.Width Petal.Length Petal.Width Species
#> 1 Bell 3.5 1.4 0.2 setosa
#> 6 Bell 3.9 1.7 0.4 setosa
Please note the way I'm sharing the sample data, that is a proper way of sharing data here.
system
Closed
October 13, 2019, 8:09pm
7
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.