Questions tagged [tidyverse]
DO NOT USE if your question relates to one or two components of the tidyverse, such as dplyr or ggplot2. Use *those* tags, and tag with `r` as well for a better response.
8,458
questions
-1
votes
1
answer
25
views
Convert character to date in R [closed]
I have a column (Date) of character with dates and I wanted to convert it to Date structure. The characters are like Jun 29, 2021 and I have applied the below code but still I get NAs for all values.
...
1
vote
2
answers
39
views
Split dataframe into multiple by every nth column
I would prefer tidyverse solution!
The question is related to this post.
Example data
structure(list(A = c(79L, 42L, 74L, 49L, 82L, 22L, 88L, 13L,
54L, 68L), B = c(41L, 22L, 1L, 40L, 96L, 48L, 40L, ...
0
votes
1
answer
27
views
change value at specific location in a tibble
I have a df
df = data.frame(col1=1:4, col2 = 5:8, col3 = 9:12)
I want to change the value in row2, col2 to 44
In base R, I use df["2","col2"] = 44, how do I do this in tidyverse?
...
1
vote
2
answers
45
views
R recode within group ID
I want to (1) create a unique group ID, and (2) recode one variable if it meets a condition within the group. I have the following data of ATM locations:
data <- tribble(
~address, ~date, ~...
-1
votes
0
answers
23
views
R tidyverse - How can I create an age group from an excisting age group
I have this dataset:
enter image description here
I need to make a new age range with the existing one and combine 25-39 with 40-59.
To make it simple to understand;
one column named age_range <- c(...
2
votes
1
answer
21
views
Summarize and count the number of unique values in grouped df with dplyr
I have this df:
structure(list(CN = c("BR", "BR", "BR", "PL", "PL", "PL",
"BR", "BR", "BR", "BR", &...
0
votes
0
answers
19
views
Creating a function to read csv and format to a particular specification in R
I’ve been wrangling many csv files individually but want to make a function so that I can read in the rest.
I would like to apply the following conditions to each of them (there are more conditions, ...
0
votes
1
answer
24
views
Filter for specific combination of two IDs [duplicate]
I have a dataframe my_df which already has some values for an ID/Date combination:
set.seed(42)
my_df <- data.frame(ID = c('A', 'B', 'C', 'A', 'B'),
Date = seq(lubridate::date('...
0
votes
2
answers
42
views
how to create a new column where the rows are determined by the previous row (calculation)?
I have this table and I want to calculate inflation for every month in every city
I tried the lag and lead functions but the CPI was overlapping onto the next city for ex : Malmo's shifted CPI was ...
1
vote
1
answer
47
views
How do I repeat the analysis of the frequency/table of categorical variables by group for multiple variables
I created this dataframe Variable_7101 but I would like to repeat this analysis for multiple variables. To be more specific, I have another dataframe (df) with rows containing the other variables that ...
0
votes
2
answers
30
views
Reshape data frame and conver values into columns
I have this dataframe:
structure(list(Segment.Number = c("Start Event", "Start Time",
"End Event", "End Time", "Segment Duration", "Total SCRs&...
2
votes
1
answer
37
views
Is it possible to iterate .data[[ ]] over a character vector in r?
I've been doing some really repetitive data analysis tasks (with lots more in the future), so I'm looking to automate as much of it as I can. I'm pretty new to R, especially creating functions, and I ...
2
votes
1
answer
15
views
Using a function on a column from tree file class Phylo
I have a phylogenetic tree with many tips and internal nodes. I have a list of node ids from the tree. These are part of a separate table. I want to add a new column to the table, children. To get the ...
0
votes
0
answers
32
views
Why do I need to include a filter on value in pivot_longer() in tidyverse?
So I have some data as follows:
id A B C D fruit word
1 1 0 0 0 apple a
2 0 1 0 0 banana a
3 1 0 0 0 apple a
4 0 0 0 1 grape v
5 0 0 0 ...
0
votes
2
answers
43
views
Use "distinct" "group_by" "summarise" two times in one pipe
I want to do something like
df1 <- iris %>% distinct(Species, .keep_all = TRUE) %>% group_by(Petal.Width) %>% summarise(Sepal.Length.mean1=mean(Sepal.Length), .groups = "drop")
...
2
votes
3
answers
49
views
How to select data from column named by other column in R
I have a dataframe in R where one column (say column A) names other columns. For example,
colA colB colC colD
'colB' 1 2 3
'colD' 4 5 6
I want to create a ...
0
votes
1
answer
31
views
Select current plot and download to file
How can I save the current plot that is displayed on the mainPanel? I am having trouble pointing the correct graphic to the download Handler. This is what I have:
library(shiny)
library(ggplot2)
...
0
votes
1
answer
56
views
How to calculate percentile in R using group by?
I have the following dataframe:
test <- tibble(
period = c(
'2019_q1',
'2019_q1',
'2019_q1',
'2019_q1',
'2019_q1',
'2019_q2',
'2019_q2',
'2019_q2',
'2019_q2',
...
3
votes
1
answer
41
views
How to rank/order bars in barchart ggplot?
I have 2 numerical (sales; cost) and 2 categorical (model; level) variables:
model level sale cost Colour
1 A HIGH 100 -110 black
2 B HIGH 120 -89 khaki1
3 C HIGH ...
3
votes
2
answers
39
views
Check if the value exists in any other columns with Tidyverse
Sample data
df <- diag(1:8) %>%
as_tibble() %>%
mutate(A = c(10, 5, 11, 4, 6, 9, 65, 8))
# A tibble: 8 x 9
V1 V2 V3 V4 V5 V6 V7 V8 A
<int> <...
4
votes
0
answers
31
views
How to add a column with an index to an apache arrow dataset in R?
I'm trying to add an index to a dataset which is too large to fit in RAM. The tidyverse way of adding an index would be:
library(tidyverse)
df = mtcars
df |>
mutate(row_id = 1:nrow(cyl)) # any ...
-1
votes
1
answer
28
views
RStudio Correlation Matrix, Error in Cor ()'y' must be numeric
I'm brand new to coding. I'm trying to create a correlation matrix for temperature, bill, pizzas, and got_wine and assign it to Q1
Q1 <- pizza %>%
select (temperature, bill, pizzas, got_wine)...
0
votes
3
answers
55
views
If value exists in another column, take value from the original column in the same row, if not nothing
Example data
library(tidyverse)
df <- tibble(
value = c(10, 50, 80, 90, 45),
value_1 = c(90, 20, 55, 30, 15)
)
# A tibble: 5 × 2
value value_1
<dbl> <dbl>
1 10 90
2 ...
0
votes
1
answer
39
views
Cross-validation in time series is returning errors, but non cross-validation method runs without errors and higher accuracy
I'm working to forecast the number of employees in the United States by month. The data is located at:
library(tidyverse)
library(fpp3)
# Source: https://beta.bls.gov/dataViewer/view/timeseries/...
-1
votes
1
answer
50
views
Using R to count how many in Dataset after filtering
Question: How many people in the dataset are age 25 or younger and are passengers? Use PERSON_TYPE and AGE to answer this question.
Code. people is the name I set for the dataset:
library(tidyverse)
...
0
votes
1
answer
35
views
Add multiple RNG columns to a tibble
db %>%
add_column(X1 = rnorm(nrow(db),0,sample(SD,1)),
X2 = rnorm(nrow(db),0,sample(SD,1))
)
I would like to automatically generate 100 random columns. I want to keep ...
2
votes
0
answers
60
views
Export multiple workbook from R
Using below image to showcase what I am trying to do. So basically I have 2 workbook with 2 sheets at a location. I want to read them in R & perform two operation (Change 0 to NA & get average ...
1
vote
1
answer
29
views
Getting column types of data in the environment via `type_convert` or `spec` functions
I have an .RDS data file where all columns are strings. I want to convert each column of this data to an appropriate type. But I also want to save the column specifications to change some of them. I ...
0
votes
0
answers
71
views
Button to ggsave graphics to a file
I have multiple graphics to render but for this example I am just showing two. I am trying to save the current graphic to a file.
How can I create a button on my shiny app to save the current graphic ...
-1
votes
0
answers
46
views
Using R to find proportion in Dataframe
Question:
Please follow the instructions in the HW description to download and
load the dataset: "Traffic_Crashes_-_People.csv".
Among female sex individuals of age 65 and above in this ...
0
votes
1
answer
42
views
Include multiple different graphics in shiny app
I am attempting to build my first shiny app. I need to include multiple graphics (about 50) and I am having problems selecting them based on their label from the dropdown control. I am able to show ...
1
vote
0
answers
49
views
Understand data masking in rlang
When reading rland document, it is still a bit of difficult for me to understand:
forwarding patterns, name patterns, bridge patterns
For example, it is difficult to judge which of following is ...
0
votes
0
answers
37
views
Evaluate an expression using named variables
I am looking for a way to evaluate an expression using temporary variables, a bit like the syntax of glue::glue(), preferentially from base R or from some tidyverse package.
For instance, in a dplyr ...
0
votes
2
answers
38
views
Subsetting a rasterbrick to give mean of three minimum months in each year
I'm interested in creating two variables from a time-series of spatial raster data in R from an netcdf file. Opening the data, subsetting by Z and creating a mean value is straight forward:
# Working ...
0
votes
1
answer
73
views
+50
R dataframe multiply row by matrix by row
I want to compute w %% Sigma %% t(w) for each row in a particular dataframe, where w is a row vector in each row of my dataframe and Sigma is a covariance matrix.
Sigma is a covariance matrix which ...
4
votes
1
answer
50
views
How to add "+" to positive values in front in the dataframe?
I have a numerical column with NAs, negative, positive, and 0 values. What I want is the following:
If the value is positive, I want to add "+" in front
If it is Negative, 0, or NA, leave ...
1
vote
1
answer
33
views
Specify which columns are the same when using map_dfr
I have two folders each with hundreds of CSVs and I want to merge them all in one data frame. I have used the following:
tbl <-
list.files(path = c("./reports_0", "./reports_1"...
0
votes
1
answer
23
views
How to get min of a date column in a data frame using tidyverse and ifelse? [duplicate]
Here is some code illustrating a behavior I don't love:
library(tidyverse)
> df <- data.frame(
+ the_date=c(as.Date("2021-01-01"), as.Date("2021-01-02")),
+ keep=c(T, T))...
0
votes
1
answer
31
views
Replacing loop with vectorization for sequential operations in R
I'm trying to use the tidyverse to avoid using a loop in my simulation. I have a dataset with 4 key variables. They're all interdependent, and each calculation needs to be handled row by row ...
0
votes
0
answers
21
views
rtudio docker tidyverse not loading
I am running docker rstudio with
docker build -t rstudio .
docker run -dp 8787:8787 rstudio
and encounter this error when attempting to install and load tidyverse and officer:
here is the error from ...
0
votes
0
answers
16
views
Pass multiple model name to map(tidy) in R [duplicate]
I am not clear how to follow this, please clarify
So, I have created 200+ models & I just want to put them in a list & pass to map(tidy), I know how to do it manually, its as below, however I ...
1
vote
3
answers
31
views
Taking two columns from your dataframe and creating a squared dataframe
Hello I have a dataframe that contains information on the correlation of two factors, which looks somewhat like this.
Factor_1 Factor_2 value
a b 0.8
a a 1
a ...
0
votes
1
answer
41
views
How to mutate across a dataframe to filter based on a two conditional cut-off value in R?
I am trying to mutate across a dataframe to filter based on a two conditions in R via tidyverse library. I would like to calculate and consider the entities passing with a cut-off value of (<= -0.5 ...
0
votes
0
answers
16
views
Behaviour of deparse and substitute when using write_csv
Had created a function that would do a bit of filtering for me, and replace anything that was not one of the primary categories with "other" that I then wanted to quickly save as an ...
0
votes
2
answers
31
views
How to rename columns by substituting suffix in R?
I have several column names in my dataset (eat10.18) that have the suffix "_10p." I'd like to change that suffix to be "_p_10" but preserve the rest of the variable name. I only ...
1
vote
1
answer
25
views
Reshape dataframe in R
I have monthly data.
df = structure(list(year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2001, 2001, 2001, ...
1
vote
2
answers
24
views
How to calculate values for the first row that meets a certain condition?
I have the following dummy dataframe:
t <- data.frame(
a= c(0,0,2,4,5),
b= c(0,0,4,6,5))
a b
0 0
0 0
2 4
4 6
5 5
I want to replace just the first value that it ...
0
votes
0
answers
21
views
Suncalc : apply to specific date and not to an interval
To echo like was made here : Can I use suncalc to create a data table with sunrise and sunset times for each date with individual geographic locations?
I have the same request and thanks for sharing ...
0
votes
0
answers
52
views
Creating an edgelist dataframe from a dataframe to create a graph in R
I am trying to construct a graph which looks like this:
The dataset looks like this:
title | text | type | userid |topic_id | rowid
------------------------------------------------...
1
vote
2
answers
46
views
Creating new columns with combinations of string patterns in R
I have a data frame - in which I have a column with a lengthy string separated by _. Now I am interested in counting the patterns and several possible combinations from the long string. In the use ...