Id = c(1:10)
age = c(14,12,15,10,23,21,41,56,78,12)
sex = c("F","M","M","F","M","F","M","M","F","M")
code = letters[1:10]
df = data.frame(Id,age,sex,code)
ex <- function (x,y,z){
temp <- df$code[y]
df$code <- df$code[z]
df[z] <- temp
}
df <- ex(df,1,3)
A = c(1:10)
B = seq(100,10,-10)
H = seq(-200,-50,along.with = B)
df = data.frame(A,B,H)
sum <- function (data,num1,num2){
SUM_x_y <- num1+num2
}
A = c(1:10)
B = seq(100, 10, -10)
H = seq(-200, -50, along.with = B)
df = data.frame(A, B, H)
df
## A B H
## 1 1 100 -200.00000
## 2 2 90 -183.33333
## 3 3 80 -166.66667
## 4 4 70 -150.00000
## 5 5 60 -133.33333
## 6 6 50 -116.66667
## 7 7 40 -100.00000
## 8 8 30 -83.33333
## 9 9 20 -66.66667
## 10 10 10 -50.00000
NEWDF = function(dataf, x, y) {
newCol = cumsum(x * dataf[, 1] + y * dataf[, 2])
dataf = cbind(dataf, newCol)
colnames(dataf)[4] = paste0("SUM_", y, "_", x)
return(dataf)
}
df = NEWDF(df, 3, 6)
df
## A B H SUM_6_3
## 1 1 100 -200.00000 603
## 2 2 90 -183.33333 1149
## 3 3 80 -166.66667 1638
## 4 4 70 -150.00000 2070
## 5 5 60 -133.33333 2445
## 6 6 50 -116.66667 2763
## 7 7 40 -100.00000 3024
## 8 8 30 -83.33333 3228
## 9 9 20 -66.66667 3375
## 10 10 10 -50.00000 3465
v =c(2,4,1,7,3,2,7,9)
double <- function(v1){
v1 <- sort(v)
vtimes2 <- 2*v1
}
v2 <- double(v1)
v2
## [1] 2 4 4 6 8 14 14 18
double <- function(a1) {
numeric_values <- as.numeric(a1)
numeric_values <- numeric_values[!is.na(numeric_values)]
if (length(numeric_values) > 0) {
numeric_values <- sort(numeric_values)
atimes2 <- 2 * numeric_values
return(atimes2)
} else {
return(numeric(0))
}
}
a <- c("a", "b", 4, 7, "q")
a <- double(a)
## Warning in double(a): NAs introduced by coercion
a
## [1] 8 14
ST = "NAME: Maria/COUNTRY:uruguay/EMAIL:mariaUY@gmail.com"
createMatrixFromST <- function(ST) {
pairs <- unlist(strsplit(ST, "/"))
matrix_data <- matrix(nrow = length(pairs), ncol = 3, byrow = TRUE)
for (i in 1:length(pairs)) {
key_value <- unlist(strsplit(pairs[i], ":"))
matrix_data[i, 1] <- trimws(key_value[1])
matrix_data[i, 2] <- trimws(key_value[2])
}
colnames(matrix_data) <- c("Attribute", "Value", "NA")
matrix_data <- matrix_data[, -3]
return(matrix_data)
}
ST <- "NAME: Maria/COUNTRY:uruguay/EMAIL:mariaUY@gmail.com"
result_matrix <- createMatrixFromST(ST)
print(result_matrix)
## Attribute Value
## [1,] "NAME" "Maria"
## [2,] "COUNTRY" "uruguay"
## [3,] "EMAIL" "mariaUY@gmail.com"
Names <- c("Tom Hastings", "Brian Wall", "Sue Klark")
Indicator <- c(TRUE, FALSE, TRUE)
Measurement <- rnorm(3)
stringdf <- data.frame(Names, Indicator, Measurement)
stringdf$NameList <- strsplit(stringdf$Names, " ")
NameDataFrame <- data.frame(
FirstName = sapply(stringdf$NameList, function(names) if (length(names) > 1) names[1] else ""),
LastName = sapply(stringdf$NameList, function(names) if (length(names) > 1) names[2] else "")
)
print(NameDataFrame)
## FirstName LastName
## 1 Tom Hastings
## 2 Brian Wall
## 3 Sue Klark