i want this code to be converted to python and r language please please convert it b 5189491

I want this code to be converted to Python and R language. Please please convert it by yourself and not by any online software or converter. Please help me I am really worried for this.

flag = 1;
for j = 1:2;
if flag == 1
Y = csvread('hd.csv',1,2);
i = 0;
else
Y = csvread('jd.csv',1,1);
Y = Y(:,1:8)
i = 5;
end

D = (Y(2:end,:)- Y(1:end-1,:));
DM = D – ones(length(D),1)*mean(D);
n = (length(D)-1);
X = DM'*DM/n;
cov(D);

if j == 2
   D = Y(2:end,:);
   DM = D – ones(length(D),1)*mean(D);
   X = DM'*DM/n
end

[FL,EVI] = eig(X)
X*FL(:,1)
EVI(1,1)*FL(:,1)

FL*EVI*FL'
X

EV = diag(EVI)
EV = EV(end:-1:1)
FL = FL(:,end:-1:1)
diag(FL'*FL)
figure(i+1)
subplot(2,1,j)
EXP = EV/sum(EV)*100
bar(EXP)
if j ==2
title('Variance Explained (Levels)')
else
title('Variance Explained (First Differences)')
end   
sum(diag(X))
figure(i+2)
subplot(2,1,j)
plot(FL(:,1:3))
if j == 2
title('First 3 Eigenvectors: Factor Loadings on Levels ')
else
title('First 3 Eigenvectors: Factor Loadings on First Differences')
end   
legend('PC1: level','PC2: slope','PC3:curvature','Location','SouthEast')
figure(i+3)
hold on
h = subplot(4,2,j)
bar(FL(:,1))
title(h,'PC1')
h = subplot(4,2,j+2)
bar(FL(:,2))
title(h,'PC2')
h=subplot(4,2,j+4)
bar(FL(:,3))
title(h,'PC3')
h=subplot(4,2,j+6)
bar(h,FL(:,4))
title('PC4')
hold off
figure(i+4)
subplot(2,1,j)
PC = DM*FL
PC(1,:).*FL(:,1)'
PC'*PC/n
diag(PC'*PC/n)
EV
mean(PC)
plot(PC(:,1:3))
if j == 1
title('Principal Components on First Differences')
else
title('Principal Components on Levels')
end
axis tight
legend('PC1: level','PC2: slope','PC3:curvature','Location','SouthEast')
figure(i+5)
subplot(2,1,j)
hold on
if j ==2
title('Levels overlayed with the 1st PC (rescaled)')
else
title('First differences overlayed with the 1st PC (rescaled)')
end   
plot(DM)
h = plot(DM*FL(:,1)/sum(FL(:,1)),'m–','linewidth',2)
legend(h,'PC1: level','Location','NorthEast')
axis tight
hold off
corr(PC)
D2 = PC*FL'
sum(sum(DM- D2))

end

figure(10+j)

hold on
h = plot(Y(2:end,:))

legend(h,'Raw Data ','Location','NorthEast')
axis tight
hold off

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now