How to conneted same dataframe column? [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 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)

Related

How do you group data with the same name in a vector? [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 10 months ago.
Improve this question
I have a DataFrame with three columns:region, year, grdp.
How do I group data with the same name in 'region' column.
Here's the code to create a sample dataset:
Here's the desired result:
store data of values with the same name in the 'region' column
ex) 'region' column has three "서울특별시" data. I want to group the three "서울특별시" data in three columns and assign it to a variable
I'm not completely understanding the question, but I think one of these two might solve what you're looking for?
library(dplyr)
df <- data.frame(region=sample(c('x','y','z'),100,replace=TRUE),
year=sample(c(2017,2018,2019),100,replace=TRUE),
GRDP=sample(200000000:400000000,100))
regions <- unique(df$region)[order(unique(df$region))]
#OPTION 1
for(i in 1:length(regions)){
assign(tolower(LETTERS[i]),df %>% filter(region==regions[i]))
}
a
b
c
#OPTION 2
ltrs <- tolower(LETTERS[1:length(regions)])
df['ex)'] <- sapply(df$region,FUN=function(x){ltrs[which(regions==x)]})
head(df)

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

How do I put vectors on the same list in R? [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
Basically I have a list of the form
data<-c("1,2,3,4","5,6,7,8")
I want to convert everything to numeric
so that the output is:
[1] 1 2 3 4
[2] 5 6 7 8
how do I do so?
This is a little unclear, but if you have this object:
d <- c("1,2,3,4","5,6,7,8")
then
d |> stringr::str_split(",") |> unlist() |> as.numeric()
will convert it to a numeric vector. If you really want it as a list then appending |> as.list() will do the trick.
If you want the result to be a list of two numeric vectors then
d |> stringr::str_split(",") |> purrr::map(as.numeric)
will work.
It is not clear, but maybe something like this:
d <- c("1,2,3,4","5,6,7,8")
d <- as.list(d)
as.numeric(unlist(strsplit(d[[1]], ",")))
[1] 1 2 3 4

how to get the count in r till it meet a specific value and continue [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
I need to write an R program that, given a vector, will return another vector giving the number of elements from each position in the input vector to the occurrence in the input vector of a specified value. For example, if the specified value is 0, then with:
input : x<-c(1,1,0,1,1,1,0)
desired output<- (2,1,0,3,2,1,0)
Thanks in advance :)
We can try
library(data.table)
ave(x, rleid(x), FUN= function(x) rev(seq_along(x)))*x
#[1] 2 1 0 3 2 1 0
We could also use rle from base R
rl <- inverse.rle(within.list(rle(x), values <- seq_along(values)))
ave(x, rl, FUN=function(x) rev(seq_along(x)))*x
#[1] 2 1 0 3 2 1 0

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