variable and value name for gather in tidyr [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 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.

Related

Why if_else function does not work in other data set [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 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.

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

Make the Day of Week Variable Binary with Baseline [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 want to make the day of the week variable binary, but also have a baseline which should be Saturday.
I have tried the following, but this does not give me my intended result at all. Is there a need to use a logical?
The range of the days of the week is 1 to 7, where 1 would be Monday.
First i renamed the numbers into the days of the week.
df$DayofW <- recode(df$dowc,
"1"="Monday",
"2"="Tuesday",
"3"="Wednesday",
"4"="Thursday",
"5"="Friday",
"6"="Saturday",
"7"="Sunday")
df$DayofW <- ifelse((df$dowc == 6), 1, -1)
The issue is that we are assigning (=) instead of comparing (==). According to ?ifelse, the first argument test
test - an object which can be coerced to logical mode.
So, it needs a comparison operator
ifelse((df$dowc == 6), 1, -1)

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))

dplyr, create a column conditional on presence or absence or text in another column [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 years ago.
Improve this question
I use dplyr. I would like to create a new column called "disease" with yes or no designation based on another column called description. If the description is NA then the value in the new column should be "N", if there is any text now in the description the value in the new column should be "Y". I tried the following code:
data%>%
mutate(disease= ifelse( is.na(Description)),"N", "Y")
There is a really simple solution using data.table
library(data.table)
setDT(data)[, disease := ifelse( is.na(cyl), "N", "Y")]
We can use base R to do this
transform(data, disease = c("Y", "N")[is.na(cyl)+1])

Resources