-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 14.8 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Replace NA values with a specific value
my_data_replaced <- replace_na(my_data, list(Age = 0, Score = 100))
print(my_data_replaced)
# Display the updated column names
print(colnames(iris))
# Change the name of the second column
colnames(iris)[2] <- "sw"
## change the cl=olumn names
# Assuming 'your_data' is your dataset
colnames(iris) <- c("SL",
"sw",
"PL",
"PW",
"SPP")
not_na_vector <- !is.na(my_data)
print(is_na_vector)
print(not_na_vector)
# Create a vector with missing values
my_vector <- c(1, 2, NA, 4, NA, 6)
# Check for non-missing values using !is.na()
not_na_vector <- !is.na(my_vector)
# Print the result
print(not_na_vector)
rm(list=ls())
## tidyverse and Pipe Operator %>%
# Using the pipe operator
result <- iris %>%
select(Sepal.Length, Species) %>%
group_by(Species) %>%
summarise(avg_length = mean(Sepal.Length))
View(result)
print (result)
iris
result_2 <- iris %>%
filter(Sepal.Length == >5 && Petal.Length > 1.5) %>%
group_by(Species)
result_2 <- iris %>%
filter(Sepal.Length == >5 && Petal.Length > 1.5) %>%
group_by(iris$Species)
result_2 <- iris %>%
filter(Sepal.Length == >5 && Petal.Length > 1.5) %>%
group_by(iris$Species)
result_2 <- iris %>%
filter(Sepal.Length == >5 && Petal.Length > 1.5)
result_2 <- iris %>%
filter(Sepal.Length == > 5 && Petal.Length > 1.5)
result_2 <- iris %>%
filter(Sepal.Length > 5 & Petal.Length > 1.5)
View(result_2)
results_3 <- iris %>%
group_by(Species)
View(results_3)
iris
results_3 <- iris %>%
sum(Species)
library(dplyr)
# Group by Species and calculate the number of individuals in each species
individual_counts <- iris %>%
group_by(Species) %>%
summarise(number_of_individuals = n())
# Print the result
print(individual_counts)
new <- apply(iris, 2, !is.na)
new <- apply(iris, 2, function = !is.na)
new <- apply(iris, 2, function(x) sum(!is.na(x)))
new
new_4 <- iris%>%
group_by(Species)%>%
summarise(number_of_in =n())
new_4
new_5 <- iris %>%
group_by(Species) %>%
mean(Species)
new_5 <- iris %>%
group_by(Species) %>%
mean(Sepal.Length, Sepal.Width)
new_5 <- iris %>%
group_by(Species)
View(new_5)
new_5 <- iris %>%
subset(Species)
new_5 <- iris %>%
apply(iris, 2, mean)
new_5 <- iris %>%
apply(iris, 2 , mean)
apply(iris, 2 , mean)
neww<- apply(iris, 2 , mean)
new <- apply(iris, 2, function(x) mean(x)))
new <- apply(iris, 2, mean(x)))
new <- apply(iris, 2, mean(x)))
new <- apply(iris, 2, mean)
new <- apply(iris, 2, colsums= mean)
new_5 <- iris%>%
summarise(iris)
new_5 <- iris%>%
summarise(iris)
summarise(iris)
summarise(iris)
iris
summarise(iris)
summarise(iris, group_by(species))
summarise(iris, group_by(iris$species))
iris
?summarise
iris%>%
summarise(mean = mean(Species), n=n())
summary_data <- my_data %>%
group_by(Species) %>%
summarise(number_of_observations = n())
summary_data <- iris %>%
group_by(Species) %>%
summarise(number_of_observations = n())
summary_data
summary_data <- iris %>%
group_by(Species) %>%
summarise(mean)
summary_data
summary_data <- iris %>%
group_by(Species) %>%
summarise(mean = mean())
mean_sepal_length <- iris %>%
group_by(Species) %>%
summarise(mean_sepal_length = mean(Sepal.Length))
mean_sepal_length
mean_sepal_length <- iris %>%
group_by(Species) %>%
summarise(mean_sepal_length = mean(Sepal.Length, Petal.Length, Petal.Width, Sepal.Width))
mean_sepal_length
mean_sepal_length <- iris %>%
group_by(Species) %>%
summarise(mean_sepal_length = mean(Sepal.Length))
mean_sepal_length
mean_values <- iris %>%
group_by(Species) %>%
summarise_all(mean, na.rm = TRUE) %>%
select(-Species)
mean_values
library(metan)
data <- data_ge
str(data)
head(data)
data$REP <- as.factor(data$REP)
data$GEN <- as.factor(data$GEN)
data$ENV <- as.factor(data$ENV)
Fitted <- lmer(GY ~ GEN + (1|ENV))
Fitted
library(lme4)
library(lme4)
library(emmeans)
library(metan)
data <- data_ge
str(data)
head(data)
data$REP <- as.factor(data$REP)
data$GEN <- as.factor(data$GEN)
data$ENV <- as.factor(data$ENV)
Fitted <- lmer(GY ~ GEN + (1|ENV))
data <- data_ge
str(data)
head(data)
data$REP <- as.factor(data$REP)
data$GEN <- as.factor(data$GEN)
data$ENV <- as.factor(data$ENV)
Fitted <- lmer(GY ~ GEN + (1|ENV))
data$GY <- as.factor(data$GY)
Fitted <- lmer(GY ~ GEN + (1|ENV))
Fitted <- lmer(GY ~ GEN + (1|ENV))
Fitted <- lmer(GY ~ GEN + (1|ENV), data = data)
library(lme4)
library(emmeans)
library(metan)
data <- data_ge
str(data)
head(data)
data$REP <- as.factor(data$REP)
data$GEN <- as.factor(data$GEN)
data$ENV <- as.factor(data$ENV)
Fitted <- lmer(GY ~ GEN + (1|ENV), data = data)
Fitted
fixef(Fitted)
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
source("~/.active-rstudio-document", echo=TRUE)
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
X <- X[!duplicated(X), ]
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
Fitted <- lmer(GY ~ GEN + (1|ENV), data = data)
Fitted
fixef(Fitted)
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
X <- X[!duplicated(X), ]
X
# Extracting the beta coefficients
Beta <- fixef(Fitted)
Beta
X <- X[!duplicated(X), ]
head(data)
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
Fitted <- lmer(GY ~ GEN + (1|ENV), data = data)
# Extracting design matrix of fixed effects (Intercept and Genotype)
X <- model.matrix(formula(Fitted))
# Obtaining the BLUEs of genotypes
BLUEs_Gen <- X %*% Beta
BLUEs_Gen
# Estimating least square means by GEN
Lsmeans_Gen <- lsmeans(Fitted, ~ GEN)
str(Lsmeans_Gen)
# Creating the data frame of GEN and BLUEs
BLUEs_Gen1 <- data.frame(GID = levels(data$GEN),
BLUEs = summary(Lsmeans_Gen)$lsmean)
BLUEs_Gen1
# BLUP of genotypes
Fitted2 <- lmer(GY ~ (1|GEN) + (1|ENV) + (1|(GEN:ENV)))
# BLUP of genotypes
Fitted2 <- lmer(GY ~ (1|GEN) + (1|ENV) + (1|(GEN:ENV)), data = data)
Fitted2
# Fixed effect=Intercept
Intercept <- fixef(Fitted2)
str(Intercept)
U_ref <- c(ranef(Fitted2)$GEN)
U_ref
U_ref
# Fixed effect=Intercept
Intercept <- fixef(Fitted2)
str(Intercept)
U_ref <- c(ranef(Fitted2)$GEN)
U_ref
# BLUP of Genotypes
BLUP_Gen2 <- Intercept + U_ref$'(Intercept)'
BLUP_Gen2
cor(BLUEs_Gen, BLUP_Gen2)
# Creating a data frame of GEN and BLUPs
BLUPs_Gen1 <- data.frame(GID = levels(data$GEN),
BLUPs = BLUP_Gen2)
BLUPs_Gen1
# Creating a data frame of GEN, BLUEs, and BLUPs
Combined_Gen <- data.frame(
GID = levels(data$GEN),
BLUEs = BLUEs_Gen1$BLUEs,
BLUPs = BLUP_Gen2
)
# Writing combined data to CSV file
write.csv(Combined_Gen, file = "BLUEs_BLUPs_Gen.csv", row.names = FALSE)
###generating random numbers
# Syntax: runif(n, min = 0, max = 1)
# Generate 5 random numbers between 0 and 1
random_uniform <- runif(5, min = 0, max = 1)
print(random_uniform)
# Syntax: rnorm(n, mean = 0, sd = 1) ####
# Generate 5 random numbers from a normal distribution with mean 0 and standard deviation 1
random_normal <- rnorm(5, mean = 0, sd = 1)
print(random_normal)
# Syntax: sample(x, size, replace = FALSE, prob = NULL)
# Sample 3 values randomly from the vector
random_sample <- sample(c("A", "B", "C", "D", "E"), size = 3, replace = TRUE)
print(random_sample)
for (variable in sequence) {
# Code to be executed for each element
}
for (i in 1:5) {
square <- i^2
print(square)
}
for (i in 2:6) {
cube <- i^3
print(cube)
}
print(cube)
i <- 1
while (i^2 < 10) {
square <- i^2
print(square)
i <- i + 1
}
n <- 1
factorial_value <- 1
while (factorial_value <= 1000) {
factorial_value <- factorial_value * n
print(paste("Factorial of", n, "is", factorial_value))
n <- n + 1
}
number <- 1
while (number^2 <= 50) {
square_value <- number^2
print(paste("Square of", number, "is", square_value))
number <- number + 1
}
library(readr)
lentil_blocked <- read_csv("C:/Users/windows/OneDrive/Desktop/lentil_blocked.csv")
View(lentil_blocked)
### loops for dataframe
data <- lentil_blocked
# Create a new column 'PRODUCT' to store the results
data$results <- numeric(nrow(data))
data
data$results[i] <- data$YIELD[i] * data$NITROGEN[i]
# Using a for loop to calculate the product of 'YIELD' and 'NITROGEN'
for (i in 1:nrow(data)) {
data$results[i] <- data$YIELD[i] * data$NITROGEN[i]
}
print(data)
data
View(data)
# Columns to include in the boxplot
columns_to_plot <- c("YIELD", "NITROGEN", "result")
columns_to_plot
# Create a new window for the boxplot
par(mfrow = c(1, length(columns_to_plot)))
# Using a for loop to create boxplots for each column
for (col in columns_to_plot) {
boxplot(data[[col]], main = col, ylab = col)
}
# Columns to include in the boxplot
columns_to_plot <- c("YIELD", "NITROGEN", "results")
columns_to_plot
# Create a new window for the boxplot
par(mfrow = c(1, length(columns_to_plot)))
# Using a for loop to create boxplots for each column
for (col in columns_to_plot) {
boxplot(data[[col]], main = col, ylab = col)
}
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(2, 2))
# Create and plot some example data
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(1, 2))
# Create and plot some example data
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(1, 3)) ## rows and columns
# Create and plot some example data
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(1, 4)) ## rows and columns
# Create and plot some example data
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")
# Columns to include in the boxplot
columns_to_plot <- c("YIELD", "NITROGEN", "results")
columns_to_plot
source("C:/Users/windows/OneDrive/Desktop/loops.R", echo=TRUE)
# Using a for loop to create boxplots for each column
for (col in columns_to_plot) {
boxplot(data[[col]], main = col, ylab = col) ##col repersents column
}
# Using a for loop to create boxplots for each column
for (col in columns_to_plot) {
boxplot(data[[col]], main = col, ylab = col) ##col repersents column
}
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(2, 4)) ## rows and columns
# Using a for loop to create boxplots for each column
for (col in columns_to_plot) {
boxplot(data[[col]], main = col, ylab = col) ##col repersents column
}
## par and mfrow function uses
# Set up a 2x2 layout for multiple plots
par(mfrow = c(1, 4)) ## rows and columns
# Create and plot some example data
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")
# Create a new column 'PRODUCT' to store the results
data$result <- numeric(nrow(data))
# Initialize a counter
counter <- 1
# Using a while loop to calculate the product of 'YIELD' and 'NITROGEN'
while (counter <= nrow(data)) {
data$PRODUCT_WHILE[counter] <- data$YIELD[counter] * data$NITROGEN[counter]
counter <- counter + 1
}
# Using a while loop to calculate the product of 'YIELD' and 'NITROGEN'
while (counter <= nrow(data)) {
data$result[counter] <- data$YIELD[counter] * data$NITROGEN[counter]
counter <- counter + 1
}
# Display the updated dataframe
print(data)
# Create a new column 'PRODUCT' to store the results
data$result <- numeric(nrow(data))
# Initialize a counter
counter <- 1
# Using a while loop to calculate the product of 'YIELD' and 'NITROGEN'
while (counter <= nrow(data)) {
data$result[counter] <- data$YIELD[counter] * data$NITROGEN[counter]
counter <- counter + 1
}
# Display the updated dataframe
print(data)
# Set a seed for reproducibility
set.seed(123)
# Number of markers and individuals
num_markers <- 50
num_individuals <- 20
# Generate SNP data (0, 1, or 2) randomly
snp_data <- matrix(sample(0:2, num_markers * num_individuals, replace = TRUE), nrow = num_individuals, ncol = num_markers)
# Convert the matrix to a data frame for better readability
snp_df <- as.data.frame(snp_data)
# Print the generated SNP data
print(snp_df)
set.seed(123)
# Function to calculate Minor Allele Frequency (MAF)
calculate_maf <- function(genotypes) {
allele_freq <- table(genotypes) / length(genotypes)
maf <- min(allele_freq)
return(maf)
}
maf
set.seed(123)
# Function to calculate Minor Allele Frequency (MAF)
calculate_maf <- function(genotypes) {
allele_freq <- table(genotypes) / length(genotypes)
maf <- min(allele_freq)
return(maf)
}
# Function to calculate heterozygotes
calculate_heterozygotes <- function(genotypes) {
heterozygotes <- sum(genotypes == 1)
return(heterozygotes)
}
# Function to calculate missing value percentage
calculate_missing_percentage <- function(genotypes) {
missing_percentage <- sum(is.na(genotypes)) / length(genotypes) * 100
return(missing_percentage)
}
# Initialize variables to store results
maf_results <- numeric(ncol(snp_df))
heterozygotes_results <- numeric(ncol(snp_df))
missing_percentage_results <- numeric(ncol(snp_df))
# Loop through each column (SNP marker) in the dataframe
for (col in 1:ncol(snp_df)) {
genotypes <- snp_df[, col]
# Calculate MAF
maf_results[col] <- calculate_maf(genotypes)
# Calculate heterozygotes
heterozygotes_results[col] <- calculate_heterozygotes(genotypes)
# Calculate missing value percentage
missing_percentage_results[col] <- calculate_missing_percentage(genotypes)
}
# Display the results
results_df <- data.frame(
Marker = colnames(snp_df),
MAF = maf_results,
Heterozygotes = heterozygotes_results,
MissingPercentage = missing_percentage_results
)
print(results_df)
View(snp_df)
maf <- min(allele_freq)
return(maf)
set.seed(123)
# defining Function to calculate Minor Allele Frequency (MAF)
calculate_maf <- function(genotypes) {
allele_freq <- table(genotypes) / length(genotypes)
maf <- min(allele_freq)
return(maf)
}
# Function to calculate heterozygotes
calculate_heterozygotes <- function(genotypes) {
heterozygotes <- sum(genotypes == 1) / sum(genotypes)
return(heterozygotes)
}
# Function to calculate missing value percentage
calculate_missing_percentage <- function(genotypes) {
missing_percentage <- sum(is.na(genotypes)) / length(genotypes) * 100
return(missing_percentage)
}
# Initialize variables to store results
maf_results <- numeric(ncol(snp_df))
heterozygotes_results <- numeric(ncol(snp_df))
missing_percentage_results <- numeric(ncol(snp_df))
# Loop through each column (SNP marker) in the dataframe
for (col in 1:ncol(snp_df)) {
genotypes <- snp_df[, col]
# Calculate MAF
maf_results[col] <- calculate_maf(genotypes)
# Calculate heterozygotes
heterozygotes_results[col] <- calculate_heterozygotes(genotypes)
# Calculate missing value percentage
missing_percentage_results[col] <- calculate_missing_percentage(genotypes)
}
# Display the results
results_df <- data.frame(
Marker = colnames(snp_df),
MAF = maf_results,
Heterozygotes = heterozygotes_results,
MissingPercentage = missing_percentage_results
)
print(results_df)
setwd("C:/Users/windows/OneDrive/Desktop/github/Genomics")