# Extract two different vectors from set in R

How can I extract the set of (a) and set of (b) as shown in the image to two different vectors, the first vector for (a) values and the second for (b) values >Solution : Using names > tmp=c("a"=1,"b"=2,"a"=3,"b"=5) > tmp[names(tmp)=="a"] a a 1 3

# Conditional mean from different column

I do have an R data frame like this: city2001 <- c(‘a’, ‘b’, ‘a’) grade2001 <- c(5, 5, 7) city2002 <- c(‘b’, ‘b’, ‘a’) grade2002 <- c(8, 9, 10) df <- data.frame(city2001, grade2001, city2002, grade2002) and would like to return , avg_a = 7.333 # from (5 + 7 + 10)/3 How is the logic… Read More Conditional mean from different column

# Mean function always returns NULL in R

The mean function is always returning NULL, no matter what I input. For example, mean(1) and mean(c(1, 2, 3)) both return NULL. I tried this on another computer and it gave me the correct values. I am using RStudio Version 2022.02.2 Build 485, "Prairie Trillium" Release for macOS and have unchecked all packages except ‘base’… Read More Mean function always returns NULL in R

# Pivot to get row and column names as rows along with values in R

I have a dataset which looks like below and i want to flatten it. inp <- matrix(c(0,1,0,0,2,3,5,0,4,3,6,0),nrow = 4, byrow = T) rownames(inp)<- c("A","B","C","D") colnames(inp) <-c("x","y","z") Expected Output: A y 1 B y 2 B z 3 C x 5 C z 4 D x 3 D y 6 >Solution : Regarding inp as a… Read More Pivot to get row and column names as rows along with values in R

# Create a function in R having these input and outputs

I want to create a function in R which summarize a vector of numbers into a dataframe representing a frequency table with k classes. The Input are: x : a vector of numeric data of any length k : an integer greater than 1 that represents the number of equal width classes dividing the support… Read More Create a function in R having these input and outputs

# Apply condition to opposite of regex pattern

How could I apply this regex so that all data in the column that does not match this regex turns to ‘Need’ – Currently, if I run this, it applies Need to all that match the regex. The regex pattern should reflect ‘A-1234567’ – Bonus: is there a way to ignore case for the first… Read More Apply condition to opposite of regex pattern

# How to change class character to class date in R without getting NA results

I’ve got a data table with a column of dates: ex_dates <- c("2022-01-01", "2022-05-28") they are now at class character, how can I change them to class date? I’ve tried this code: data\$date <- as.Date(data\$date,"%d/%m/%Y") but it changes the whole column to NA. >Solution : You can use lubridate package with the function YMD: class(data\$date)… Read More How to change class character to class date in R without getting NA results

# Vectorised argument for a function in R. The function gives out multiple data frames, whereas I'd like it to output only one

I’d like to compute trimmed mean for each trimming proportion alpha, and then see which trimming proportion gives the minimal variance of the trimmed means, when Bootstrap simulations of size N=200 are applied. The problem that I have, is that when I try to create a data frame of column1 = mean and column2 =… Read More Vectorised argument for a function in R. The function gives out multiple data frames, whereas I'd like it to output only one

# Perform arithmetic operation with variables at t and t-1 (R)

I want to perform the following mathematical operation between two variables at different time periods of the same dataframe: lag_var = var1(t)/var2(t-1) > df var1 var2 lag_var 1 10 NA 3 12 0.3 1 16 0.083 6 19 0.375 16 7 0.84 Any ideas? >Solution : You can achieve that via the lag() function and… Read More Perform arithmetic operation with variables at t and t-1 (R)