Add values in a cell R [closed] - r

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

Related

Create A Vector in R with size n+1 [closed]

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 create an integer vector in R with size n+1, but R cant understand that with "n" i mean all the natural numbers.
Something like this:
n = 1:10
my_vector <- n+1
my_vector
[1] 2 3 4 5 6 7 8 9 10 11

Using elements of the column to calculate profitability [closed]

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

create an extra column based on 16 other numeric columns [closed]

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 have 16 variables that are numeric, and I need to create an extra column that is YES (otherwise NO) when 3 or more variables out of those 16 have a value above 1015.
How could I do that?
Thanks
You can try with rowSums :
cols <- 1:16
df$res <- ifelse(rowSums(df[cols] > 1015, na.rm = TRUE) >= 3, 'Yes', 'No')

How to sort data by using loops? [closed]

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 3 years ago.
Improve this question
I want to write a function that rearranges a vector in ascending or descending order. I know I can use sort and order functions but I want to do it manually.
If you want to practice writing your own sorting function, here is a example which applies a recursion approach:
mysort <- function(v, descending = F) {
if (length(v)==1) return(v)
if (descending) return(c(max(v),mysort(v[-which.max(v)],descending = descending)))
return(c(min(v),mysort(v[-which.min(v)])))
}
EXAMPLE
v <- c(1,2,5,4,2,7)
# ascending manner
mysort(v)
# descending manner
mysort(v,descending = T)
such that
> mysort(v)
[1] 1 2 2 4 5 7
> mysort(v,descending = T)
[1] 7 5 4 2 2 1

R : how to get rid of all NaN values and replace them by 0 in a complex function / R file [closed]

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 7 years ago.
Improve this question
How is it possible to get rid of all NaN values and replace them by zero (0) in a complex function / whole R file?
I'm not sure about what you mean by a whole file/complex function, but depending on the data type you're storing the file with, it's pretty easy using is.na():
df <- data.frame(A = rep(1, 5), B = rep(1,5))
df$B[1] <- NA
df$A[3] <- NA
df[is.na(df)] <- 0

Resources