4,370 questions
0
votes
2
answers
80
views
R - Sum rows from different column based on condition in grouped values
I have a large dataset of different sites inside and out of Natura 2000 network.
An example:
df
Name
Total_Surface
N2000
SurfaceN2000
A
1
Yes
0.5
B
5
No
NA
C
11
No
NA
D
10
Yes
5
I want to have ...
0
votes
1
answer
27
views
Order dataframe within pivot_wider function?
I have a dataframe in a longlist format with duplicate IDs. Each ID has a so-called donornr and timepoint (Tijdspunt). One ID (Deelnemernr.) can have duplicate timepoints like so:
Deelnemernr. ...
1
vote
0
answers
50
views
data wrangling help in assigning items across lists using dplyr
have a dataset containing items to be used in a psychology experiment. Counterbalancing is crucial for this experiment. I need to arrange the items across 4 lists. I would appreciate your help in ...
2
votes
2
answers
59
views
Alternatives to reshape2::melt() for matrices with named rows/columns? [duplicate]
The melt() function from the reshape2 package has a convenient behavior where it will convert a matrix with named rows/columns into a three-column data frame with the matrix row/column names becoming ...
0
votes
0
answers
24
views
How do I group columns with R tidyr's complete() function? [duplicate]
library(tidyverse)
df <- tibble(alph = c("a", "b", "b"),
nums = c("1", "2", "2"),
vals = c("foo", &...
0
votes
1
answer
22
views
pivot_longer() with parallel (unlinked) sets of columns [duplicate]
I'm trying to use pivot_longer() to rearrange a dataset I was given, which looks like the result of a database join operation. Here's an example of what it looks like:
dat <- tibble('Plant_Name'=c('...
-2
votes
1
answer
66
views
Pivot a range of columns based on a character string in R
Assume a data frame like this:
df <-
data.frame(
total_income = c(100, 500, 2000),
ten_twenty_k = c(90, 480, 300),
twenty_one_thirty_k = c(10, 20, 1700),
total_age =...
0
votes
1
answer
47
views
how to unnest json string from data frame in R?
Hi and thanks for reading me. I have a data frame that have the next structure:
# A tibble: 6 × 2
customerId transactionHistory ...
0
votes
1
answer
37
views
Tailor gaze data by negative transition time
I work with gaze data (here, aoi_C_aoi giving gaze fixations and aoi_C_dur giving the durations of these gaze fixations) during questions (Utterance) in conversation. The issue I am grappling with is ...
4
votes
3
answers
75
views
How to fill NA's in a column if the column contains a number but leave NA if a column is all NAs
I am creating a file for running in a model with species counts at a number of different sites that we visited multiple times. However, not all the sites were visited every visit.
I need a column to ...
0
votes
1
answer
29
views
In package tidyr , question about `separate_longer_delim` can't recycle input
In below code , separate_longer_delim return error message
In row 2, can't recycle input of size 4 to size 6.
how to fix it ? Thanks!
library(tidyverse)
ori_df <- data.frame(
Cat_A = c("...
0
votes
0
answers
50
views
R: row_number not working correctly with pivot_wider? [duplicate]
I’m getting stumped on this one. I have a data frame that I’m trying to reshape into wide form using pivot_wider, but the results aren’t as expected.
My data is currently structured like this:
One ...
1
vote
1
answer
71
views
eval_tidy() working for normal functions but not tidyverse functions
I am trying to write a function that conditionally applies a sub-pipeline within a dplyr-pipeline but I am stuck at applying the sub-pipeline to the data.
I tried all kinds of defusions with quosures ...
0
votes
1
answer
31
views
Advanced pivot_longer transformation sequentially on a group of columns
I'm a little perplex concerning the exact way to proceed with this wrangling procedure.
I've a dataset which consist in raters that are assessing lung sounds (S1,...,S40). For each sound the assessed ...
2
votes
1
answer
53
views
in dplyr::mutate, dplyr::starts_with works for .before but not .after?
Can someone explain why, in the below context, mutate .before starts_with works but not mutate .after starts_with?
dfr_before <- data.frame(old1=1, old2=1, prefix_old1=1, prefix_old2=1, old3=1)
...
0
votes
0
answers
38
views
pivot_longer across multiple columns and type: Can't combine <datetime<UTC>> and <double> [duplicate]
I'm trying to pivot_longer across multiple columns with multiple types. They have a common prefix and I need to keep the value after the underscore.
Reprex data:
dat <- structure(list(id = c(230....
4
votes
2
answers
86
views
Is there a R function to pair elements from two columns of a data.frame to make a new data.frame
I have several thousand rows like this stored in a object in R. I want to make a new df2 by pairing elements from two columns ($exonStarts and $exonEnds) from the df below
df <- structure(list(bin =...
3
votes
1
answer
60
views
Reformat data to include 2 minute time intervals
Example data are as follows:
library(dplyr)
library(lubridate)
library(tidyr)
set.seed(123)
dummy_data <- tibble(
focalbear_event = rep(c("event1", "event2"), each = 10),
...
1
vote
3
answers
92
views
Tidy up datasets - converting subheaders into column
I received a txt data file that looks like this:
# test A response c
1 x1 1
2 x2 0
.. ..
324 x324 5
# test P response 8
1 x1 2
2 x2 1
.. ..
501 x501 4
# test 7 ...
3
votes
2
answers
60
views
Quick way to combine several pairs of columns into one column for each pair [duplicate]
In R I am trying to combine pairs of columns in order to find the maximum value between the pairs of columns.
My dataframe is structured like this:
df <- data.frame(id = c("u1", "u1&...
1
vote
1
answer
41
views
How to create an empty named specified column and position this column into a particular numbered position within the data frame in R
I have a data frame with 388 columns, and I have two columns in my data frame labelled as 'Longitude_E' and 'Latitude_N', containing GPS coordinates in degrees, minutes, and seconds.
I aim to create ...
3
votes
2
answers
63
views
Separate string column of varying lengths and multiple delimiters into new columns using tidyr
I would like to separate the prod_code_date column (a string of varying lengths, from one "pair" to multiple "pairs") into multiple columns based on the delimiter ":" and ...
1
vote
1
answer
61
views
complete data frame with same number of rows per group
I’m struggling with an issue regarding the complete() function that I can’t resolve. I managed to have the desired behaviour another dataframe but I can’t make it work the same way for this one even ...
1
vote
1
answer
72
views
Unnest list column: In row 2, can't recycle input of size 2 to size 4
I'm encountering issues when attempting to unnest a dataframe containing list columns. The dataframe structure and the unnesting code I'm using are as follows:
library(dplyr)
library(tidyr)
df = ...
4
votes
2
answers
91
views
Separate collapsed column into multiple rows, preserving grouping information
I have a messy table I read into a data frame similar to the following simplified one, which I want to parse.
mydf <- data.frame(group=LETTERS[1:5], code=101:105, ids=c('g1:id1,id2,id3\ng2:id4,id5',...
0
votes
1
answer
55
views
How to transform and pivot a rather messy table in R using tidyverse?
I have a table with approximately 2000 entries that contains names, positions, field of expertise, and addresses of professors. The table is quite messy, and I’m struggling to find a programmatic way ...
1
vote
3
answers
72
views
R separate_longer_delim to split strings with quotation marks
I am trying to use the separate_longer_delim() function from the tidyr package to split a string into separate rows.
This is how the dataset looks like:
Subject
Outcome
001
["Rubbing of nose or ...
2
votes
4
answers
102
views
fill NAs with set of preceding values
I have a dataframe that looks like this:
d = data.frame(obs = 1:4, comp = c("7_10","5","6_9","1_2_6_9"))
obs comp
1 1 7_10
2 2 5
3 3 ...
4
votes
5
answers
123
views
Most idiomatic way to mutate multiple similar columns?
I'm generating multiple columns in a mutate() call, all by using a function of 1) an already-existing column and 2) some value which is different for each output column. The following code produces ...
1
vote
1
answer
36
views
ggplot line plots are not showing up on proportional secondary axis
I am trying to plot a barplot, and overlay some plots onto it using a secondary axis. I am able to adjust the secondary axis to be proportional to the values but the line plots still seem be squished ...
0
votes
1
answer
106
views
Complex lon/lat format manipulation
I originally developed a model to work with a df as df1_ref. Now, I found more data I need in a different format as df2.
df1_ref:
id country point_id lon lat value_A
...
0
votes
1
answer
43
views
R How to reframe data to connect start and stop times
I have a data set that describes narwhal behaviours. There are eight whales (whaleID), multiple behaviours (behaviour), each whale can interact with none, some or all of the other whales (modID), and ...
2
votes
3
answers
94
views
Complete and fill missing rows with groups of uneven length
I have a dataframe of county executives and the year they were inaugurated. I am running a panel study with county-year as the unit of analysis. The date range is 2000 to 2004.
I will like to expand ...
1
vote
2
answers
121
views
Pad column numbers while using separate_wider_delim in R
I have a dataset in R that contains a column that contains a string that I want to separate into multiple columns using separate_wider_delim from the tidyr package.
What I want to do is to pad the ...
3
votes
4
answers
93
views
Convert a dataframe of nearest neighbors to onehot coding
Let's say we took the mtcars data and ran a PCA. Then, we want to know which brands of cars are most similar in PC space, i.e. the nearest neighbors. So someone ran a nearest neighbors analysis and ...
1
vote
2
answers
78
views
parallel/automatic way of unnesting list columns that contains data frames (list columns might be empty)
Please consider the following data frame:
df <- structure(list(oID = c(37751L, 30978L, 33498L),
peId = c(12L, 13L, 14L),
last_Name = c("ABC", &...
1
vote
4
answers
133
views
Apply function rowwise() using column names to identify function arguments using mutate()
I've created a tibble where each column is named to correspond to the arguments of a custom function. There are many columns and so I'm trying to avoid having to explicitly call and assign each ...
0
votes
1
answer
58
views
Change raster extent with tidy
I have files from ERA5 that have extent from 0 to 360 (lon) and -90 to 90 (lat)
Example:
> era5_sr
class : SpatRaster
dimensions : 721, 1440, 744 (nrow, ncol, nlyr)
resolution : 0.25, 0....
1
vote
0
answers
70
views
Selecting columns based on multiple conditionals (including string matching) using dplyr
Having fun testing out the capabilities of the select_if() function, especially learning how to bundle multiple conditionals together for column selection.
For example I can select factors that have ...
1
vote
1
answer
49
views
Pivot distinct value pairs wider using timestamp
I have distinct pairwise data points in column Utterance; the data pairs have identical Timestamps. I'd like to pivot the distinct Utterance pairs wider and align them on the same row with the same ...
1
vote
1
answer
38
views
pivot_longer from multiple columns into a singular names_to and two values_to
I've spent most of the day on this and finally calling in some help. There are multiple entries here on related questions, but none that quite get at what I'm trying to do.
Below is an example df.
x &...
3
votes
3
answers
59
views
How can I convert two summarized columns to two columns with a binary response in R?
I have a dataframe in R with results of sports matches. Each row represents one match between two players with columns for the date of the match, the player names, and the scores of each player.
...
0
votes
2
answers
32
views
How to avoid input variable names in tidyr::complete()
In tidyr::complete, I have to input variable names, how to avoid it? In actual, the variable are some many, it's boring to input one by one ... Anyone can help?
library(tidyverse)
raw_df <- ...
0
votes
1
answer
44
views
Force column to appear when no data returned for group after summarising dataframe
I have a (non-shareable) data frame of cases of disease (each row is a person with a disease) and columns corresponding to their age group (Age_Grp_15) and their quintile of marginalization (...
4
votes
1
answer
62
views
substringing {.col} from mutate(across()) on the fly
Say I have the following data:
df <- structure(list(treat = structure(1:4, levels = c("Control", "Treatment 1",
"Treatment 2", "Treatment 3"), class = "...
0
votes
1
answer
52
views
Compare each unique name-value combination to name-value combinations with other names
In R, I have a data-frame with unique name-value combinations:
data <- data.frame(
name = c('a', 'a', 'b', rep('c', 3)),
value = c('d1', 'd12', 'd123', 'b1', 'c12', 'd1234')
)
> data
name ...
0
votes
6
answers
174
views
How to determine which column caused an error
I have a data.frame with many list columns.
df <-
tibble::tibble(
a = list(c(1,2), c(3,4)),
b = list(c(1, 2), 'a')
)
I would like to unnest() all of them at once. However, one of ...
2
votes
1
answer
109
views
How to use the ptype argument in unnest_longer()
I have a data.frame that looks like this.
library(tidyverse)
# Create a data frame with a list-column
df <-
tibble(
id = c(1,2),
lst = list(c(1,2), c('a', 'b'))
)
df
#> # A tibble: ...
0
votes
1
answer
96
views
how do split or separate character strings in rows of a column [duplicate]
library(rvest)
library(tidyverse)
url = 'https://en.wikipedia.org/wiki/2023_Nigerian_House_of_Representatives_election'
html_content = read_html(url)
tables = html_table(html_content, fill = TRUE)
...
4
votes
2
answers
86
views
Fill first (only one) NA with next non-NA value by group using dplyr/tidyr
I hava a problem, where I need to fill NA by group, but only one (the first) NA before some non-NA value (direction = "up"). I know the tidyr fill() function but do not know how to use it ...