Order of operations in summarise [closed] - r

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
What is happening in the first line of code and why does the result differ from the two next results?
library(tidyverse)
library(magrittr)
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B))
yields D=0
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(A = sum(A),B = sum(B), D=sum(A-B) )
yields in D=2
data.frame(A=c(2,2),B=c(1,1)) %>%
summarise(sum_A = sum(A),sum_B = sum(B), D=sum(A)-sum(B))
yields in D=2.
I can't seem to come up with an explanation to how the D=0 can be a result of such an operation. How can D=0 be a sensible result?

It is a bug, see https://github.com/tidyverse/dplyr/issues/3233. It is fixed in 0.7.4.9001.

Related

R basics and questions [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I have assigned a value for a variable but still am getting an error message. Kindly help me rectify
Code:
n <- 1000
x <- seq(1,n)
sum(x)
Error: object 'x' not found
You either need to use a new line for each command or end each command with semicolon (";")
n <- 1000
x <- seq(1,n)
sum(x)
Or:
n <- 1000; x <- seq(1,n); sum(x)

In my CSV file, it shows I have 1 column when I actually have 15 columns [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
> df <- read.csv("DATA ONLY.csv", header = TRUE, sep = ";")
> dim(df)
[1] 439 1
This is the code I use and this is the CSV
https://docs.google.com/spreadsheets/d/1SOqDKXZ7BAMW5LdqBcBIvQE9_PnFcNIHDfYDty3cTto/edit?usp=sharing
I am 99% sure that you have defined the field separator wrong. data.table::fread is really good at sniffing the correct format of csv's, and I quite often use fread even if I just convert the resulting data.table back to vanilla data frame, i.e.
library(data.table)
df <- fread("DATA ONLY.csv")
as.data.frame(df) -> df

A wierd problem that group_by() doesn't work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a dataset with 3 factor columns and 4 numeric columns. I want to use group_by() to summarize it. But no matter how I try it doesn't work, there is no group.
freetick <- read.csv("FreeTickAll.csv", stringsAsFactors=FALSE)
library(dplyr)
group1 <- freetick %>% group_by(Habitat, Month) %>% summarize(
meanAd = mean(Adult),
meanNy = mean(Nymph),
meanLa = mean(Larva)
)
group1
The result:
> group1
meanAd meanNy meanLa
1 0.6129032 4.258065 20.1129
And my group1 data.frame also show:
mean Ad mean Ny mean La
1 0.6129032 4.258065 20.1129
If a function is common in multiple packages and those packages are loaded into the working env, then there is a possibility of masking the function from the last loaded package. In such cases, either restart the R session with only the package of interest loaded (dplyr in this case) or specify the function to be loaded explicitly from the package of interest (dplyr::summarise)
freetick %>%
dplyr::group_by(Habitat, Month) %>%
dplyr::summarise(meanAd = mean(Adult),
meanNy = mean(Nymph),
meanLa = mean(Larva))

If statement in R, multiple conditions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wonder how I can write this if statement correct. I have tried a lot of things, but none of them worked.
b <- matrix(NA,10,10)
> for (row in 1:10)
>> for (column in 1:10)
>>> if(!is.na(a[row,column] && a==(1 || 2 || 3))
b[row,column]==1
>>> else
b[row,column]==0
The problem is here:
if(!is.na(a[row,column] && a==(1 || 2 || 3))
Assuming that 'a' is a matrix with the same dimension as 'b', we can do this without any if/else
+((a %in% 1:3) & !is.na(a))
data
set.seed(24)
a <- matrix(sample(c(1:9, NA), 10*10, replace = TRUE), 10, 10)

variable and value name for gather in tidyr [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to use gather function of tidyr package to convert wide data into a long one with following demo.
library(tidyr)
library(dplyr)
messy <- data.frame(
name = c("Wilbur", "Petunia", "Gregory"),
a = c(67, 80, 64),
b = c(56, 90, 50)
)
gather(messy,drug, heartrate, a:b)
Although I have used drug and heartrate to indicate condition and value, however, it still names as the default one variable and value. Why?
It must be something related with masking of functions. It should work when you rerun the code on a fresh R sessioin with only tidyr and dplyr loaded.

Resources