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))
Related
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 months ago.
Improve this question
I am running an if_else function to create a new outcome vectors from 4 columns of data.
The command is as follows:
payment_amt <- if_else( interest_rate>0,
(balance-(balance*amortisation_factor)/(1+(interest_rate/12))^tenor)*((interest_rate/12)/(1-((1+(interest_rate/12))^(-1*tenor)))),
0 )
This command work well in 1 of my data
But does not work in other data
I tried my best to google but could not understand why the command did not work for the second set of data.
Very much appreciate if anyone can help!
Here I attach here my code & the data_work and data_not_work sets for your reference
# Data Work _ test
tenor = data_work[,"ECL_TENOR"]
interest_rate = data_work[,"INTEREST_RATE"]
amortisation_factor = data_work[,"AMORTISATION_FACTOR"]
balance = data_work[,"ECL_BALANCE"]
payment_amt <- if_else( interest_rate>0,
(balance-(balance*amortisation_factor)/(1+(interest_rate/12))^tenor)*((interest_rate/12)/(1-((1+(interest_rate/12))^(-1*tenor)))),
0 )
payment_amt
#####################################################
# Data Not work _ Test
tenor = data_not_work[,"ECL_TENOR"]
interest_rate = data_not_work[,"INTEREST_RATE"]
amortisation_factor = data_not_work[,"AMORTISATION_FACTOR"]
balance = data_not_work[,"ECL_BALANCE"]
payment_amt <- if_else( interest_rate>0,
(balance-(balance*amortisation_factor)/(1+(interest_rate/12))^tenor)*((interest_rate/12)/(1-((1+(interest_rate/12))^(-1*tenor)))),
0 )
Here is data
After posting this question, I found out that during the merging process, the data_not_work set has been hiddenly converted to tible, that why if_else does not work. When I convert it back to data frame, then if_else work.
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 1 year ago.
Improve this question
Input:
library("UsingR")
library("dplyr")
data("kid.weights")
attach(Kid.weights)
df <- data.frame(gender[1:6],
weight[1:6],
height[1:6],
Kg=weight[1:6] * 0.453592,
M=height[1:6] * 0.0254)
df
df %>%
group_by(df$gender) %>%
summarise(mean(df$weight))
Output:
> df %>%
+ group_by(df$gender) %>%
+ summarise(mean(df$weight))
# A tibble: 2 x 2
`df$gender` `mean(df$weight)`
<fct> <dbl>
1 F 58.3
2 M 58.3
I want to make data frame for mean(weight(kg)) or median(weight(kg)) to gender.
but it is not working. looks like.
how to it solve?
Once you use %>% you don't need to reference to df anymore:
df %>%
group_by(gender) %>%
summarise(mean(weight))
%>% is a pipeline which makes you accessible to the columns directly, on each group, df$gender and df$weight would give you the whole column.
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
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 5 years ago.
Improve this question
I have the following kind of data in my csv file
DriveNo Date and Time Longitude
156 2014-01-31 23:00:00 41.88367183
187 2014-01-31 23:00:01 41.92854
These data have a lot of noise. Sometimes, a driver(the DriveNo is unique) is present in two different locations at the same time , which is not possible and a noise. I tried to do it using distinct(select(five,DriveNo,Date and Time))
but i get the following error
Error: unexpected symbol in "distinct(select(five,DriveNo,Date and"
However, when i try
distinct(select(five,DriveNo,Longitude))
it works.But, i need it with DriveNo and Date and Time.
you can escape with backticks, like:
df %>%
select(DriveNo, `Date and Time`, Longitude) %>%
distinct()
or using group_by, like:
df %>%
group_by(DriveNo, `Date and Time`) %>%
select(Longitude) %>%
unique()
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.