No description has been provided for this image

Open In Colab

Vectors in R and Python¶

In [ ]:
## if needed, run this and RESTART KERNEL
# import os
# os.environ['R_HOME'] ='/Library/Frameworks/R.framework/Resources'
In [ ]:
%load_ext rpy2.ipython

1. Creating¶

In [ ]:
import numpy as np

namesP=np.array(["Qing", "Françoise", "Raúl", "Bjork","Marie"])
agesP=np.array([32,33,28,30,29])
countryP=np.array(["China", "Senegal", "España", "Norway","Korea"])
educationP=np.array(["Bach", "Bach", "Master", "PhD","PhD"])
In [ ]:
namesP
In [ ]:
%%R
namesR=c("Qing", "Françoise", "Raúl", "Bjork","Marie")
agesR=c(32,33,28,30,29)
countryR=c("China", "Senegal", "España", "Norway","Korea")
educationR=c("Bach", "Bach", "Master", "PhD","PhD")
In [ ]:
%%R
namesR

2. Accessing¶

In [ ]:
# Keep in mind the positions in Python start in **0**.
agesP[0]
In [ ]:
%%R
agesR[1]
In [ ]:
#last element
countryP[-1]
In [ ]:
%%R
tail(countryR,1)

Slices¶

In [ ]:
# several, using slices:
agesP[1:4] #second to fourth
In [ ]:
%%R
agesR[2:4]
In [ ]:
# non consecutive

agesP[[0,2,3]]
In [ ]:
%%R
agesR[c(1,3,4)]

Replacing¶

by Position¶

In [ ]:
countryP[2]="Spain"

# list changed:
countryP
In [ ]:
%%R
countryR[3]="Spain"

# list changed:
countryR

By Value¶

In [ ]:
countryP=np.char.replace(countryP, 'China', 'PR China')
countryP
In [ ]:
%%R
countryR[countryR=='China']="PR China"
countryR

Deleting¶

By Position¶

In [ ]:
countryP=np.delete(countryP,-1)
countryP
In [ ]:
%%R
countryR=countryR[-length(countryR)]
countryR
In [ ]:
# for several positions

vectorP=np.array([1,2,3,4,5,6])
vectorP=np.delete(vectorP,range(1,4))
#now:
vectorP
In [ ]:
%%R

vectorR=c(1,2,3,4,5,6)
vectorR=vectorR[-c(2:4)]

#now:
vectorR
In [ ]:
# non consecutive
byeIndex=[1, 3, 5]

vectorP=np.array([11,22,33,44,55,66])
new_vectorP=np.delete(vectorP,byeIndex)

#now:
new_vectorP
In [ ]:
%%R
vectorR=c(11,22,33,44,55,66)
new_vectorR=vectorR[-c(2,4,6)]
new_vectorR

by Value¶

In [ ]:
# by value
vectorP=np.array([1,3,45,3,3])

vectorP=np.delete(vectorP, np.where(vectorP == 3))

#  changed:
vectorP
In [ ]:
%%R

vectorR=c(1,3,45,3,3)
vectorR=vectorR[vectorR!=3]
vectorR
In [ ]:
# by value
vectorP=np.array([1,3,45,3,3])

#vectorP=
vectorP=np.delete(vectorP, np.isin(vectorP,[1,45]))

#  changed:
vectorP
In [ ]:
%%R

vectorR=c(1,3,45,3,3)
vectorR=vectorR[!vectorR%in%c(1,45)]
vectorR

Inserting values¶

at the End¶

In [ ]:
vectorP=np.array([1,2,3,4])
vectorP=np.append(vectorP,5)
vectorP
In [ ]:
%%R
vectorR=c(1,2,3,4)
vectorR=c(vectorR,5)
vectorR

in a particular location¶

In [ ]:
vectorP=np.array([1,2,3,4])
vectorP=np.insert(vectorP,obj=1,values=10) #obj is position
vectorP
In [ ]:
%%R
vectorR=c(1,2,3,4)
vectorR=append(vectorR,10,after=1)
vectorR

Concatenating¶

In [ ]:
import numpy as np

vectorP1=np.array([1,2,3,4])
vectorP2=np.array(['a','b'])
vectorP12=np.concatenate((vectorP1,vectorP2))
vectorP12
In [ ]:
%%R
vectorR1=c(1,2,3,4)
vectorR2=c('a','b')
vectorR12=c(vectorR1,vectorR2)
vectorR12

Remember that vectors support just one data type, and coercion will activate when different data types are detected.

In [ ]:
vectorP1=np.array([1,2,3,4,"O"])
vectorP1
In [ ]:
%%R
vectorR1=c(1,2,3,4,"o")
vectorR1