Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to calculate the logarithmic profitabilities of a stock asset. The formula for this calculation is:
ln(Row t+1/row t)
and I want to do this in R. Is it possible?
This is a dirt example of the concept. I hope all you understand it
Thanks in advance
This could be a very basic solution in base R using a for loop:
Date <- c("01-01-2022", "01-02-2022", "01-03-2022")
Date <- as.Date(Date, format = "%d-%m-%Y")
Price <- c(2, 3, 5)
df <- data.frame(Date, Price)
df$Profitablity <- rep(NA, nrow(df))
for(i in 2:nrow(df)) {
df$Profitablity[i] <- log(df$Price[i]/df$Price[i-1])
}
df
Date Price Profitablity
1 2022-01-01 2 NA
2 2022-02-01 3 0.4054651
3 2022-03-01 5 0.5108256
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to create a vector of only 0s and 1s. I know how to do this, however, I would like to control the sum value as well as the length of the vector. How could I do this?
The following creates a vector of length len, and then randomly select n_sum locations for which 1 is assigned.
len <- 10
n_sum <- 7
vec <- numeric(len)
vec[sample(1:len, n_sum, replace = FALSE)] <- 1
sum <- 100
length <- 121
my_vector <- c(rep(0,length - sum),rep(1,sum))
> length(q)
[1] 121
> sum(q)
[1] 100
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i want connect column in same dataframe.
for example,
# I have data type is below
region=c("A","B","C")
Q1=c("ads","qwer","zxcv")
Q2=c("poi","lkj","mnb")
temp=data.frame(region, Q1, Q2)
### i want chaged below
region1=c("A","B","C")
Q=c("ads,poi","qwer,lkj","zxcv,mnb")
temp2=data.frame(region1, Q)
How to do it... ?
temp$Q <- apply(temp[-1], 1, toString)
temp[c("Q1", "Q2")] <- NULL
temp
region Q
1 A ads, poi
2 B qwer, lkj
3 C zxcv, mnb
Using base R you can do:
temp$Q <- paste(temp$Q1, temp$Q2, sep=",")
temp <- temp[,c("region", "Q")]
temp
region Q
1 A ads,poi
2 B qwer,lkj
3 C zxcv,mnb
This would be a solution using the mutate function from the dplyr package to create the new column Q by using paste0 to concatenate the columns Q1 and Q2. In the end I just removed the columns Q1 and Q2 by using select with -:
library(dplyr)
temp %>% mutate(Q = paste0(Q1,", ",Q2)) %>% select(-Q1,-Q2)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I got an column with the types of beds that are in a hotelroom, but I want to know how many people can sleep in this room. My dataset looks like this:
nrofP
2||2
3||3
1
6
1||1
2||2||2||2||2
5
2||2||1
My expected outcome is:
nrofP
4
6
1
6
2
10
5
5
I hope you might know a way to fix my problem.
rooms <- c("2||2", "3||3","1", "6", "1||1", "2||2||2||2||2",
"5","2||2||1")
occupancy <- do.call("c", lapply(strsplit(rooms, "\\|\\|"), function(x)
{
sum(as.numeric(x))
}))
occupancy
Considering nrofP as a vector.
sapply(nrofP,function(x) sum(as.numeric(unlist(strsplit(gsub('\\|\\|',' ',x),' ')))))
library(lazyeval)
df <- data.frame(nrofP = c("2||2", "3||3", "1", "2||2||1"), stringsAsFactors = F)
df$nrofP <- lapply(gsub("||", "+", df$nrofP, fixed = T), function(x) lazy_eval(x))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried to plot sales order against time. graph is as follows:
I want to replace number of days by dates:
which means replace 0 by 22/09/2016
50 by 11/11/2016
100 by 31/12/2016
150 by 19/02/2017
200 by 10/04/2017
I don't know how to proceed.
You will want to make use of the xts package which can handle daily values
library(xts)
v <- 0:200 # your data here
d <- seq.Date(as.Date('2016-09-22'), as.Date('2017-04-10'), length.out = 201)
plot(xts(v, order.by = as.POSIXct(d)))
Here is a great cheat sheet:
https://www.datacamp.com/community/blog/r-xts-cheat-sheet#gs.1XjXRyI
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to work out the difference between dates between two date columns in a dataframe df$started and df$done. The result is put in a third column called df$diff Some of the df$diff has NA and in these rows I want to enter the difference between the current date and df$started. How can I do this?
In the future, please follow the questions guidelines.
library(lubridate)
start = as.Date(c("14.01.2015", "26.03.2015"),format = "%d.%m.%Y")
end = as.Date(c("18.01.2015", NA),format = "%d.%m.%Y")
diff = ifelse(!is.na(end),difftime(end,start,units="days"),difftime(Sys.time(),start,units="days"))
df = data.frame(start,end,diff)
View(df)
start end diff
1 2015-01-14 2015-01-18 4.0000
2 2015-03-26 NA 174.7846