Skip to content

fixing error with dataframe size #204

Merged
merged 4 commits into from Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified StudentData/PIXL_LIBS_Combined.Rds
Binary file not shown.
142 changes: 68 additions & 74 deletions StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd
Expand Up @@ -90,11 +90,8 @@ libs.df[,6:13] <- sapply(libs.df[,6:13],as.numeric)
libs.df<-libs.df%>%
filter(!(grepl("scct", target)))
#add a column to indicate the nearest pixl
libs.df<-cbind(nearestpixl=0,libs.df)
#make a dataframe of just the LIBS Lat/Long and target name and remove duplicates
libstargets.df<-libs.df[,c(1,3,4,5)]
libstargets.df<-cbind("nearestpixl"=0,libs.df[,1:4])
libstargets.df<-distinct(libstargets.df)
```

Expand All @@ -120,33 +117,24 @@ pixl.df<-pixl.df[c(2,4,6,8,10,12,14,16),]
### Combining datasets

```{r}
distance=100
distCosine(pixl.df[1,c(1,2)],libstargets.df[1,c(2,3)], r=3393169)
for(i in 1:nrow(pixl.df)) {
libstargets.df<-libstargets.df%>%mutate(nearestpixl = ifelse(distHaversine(pixl.df[i,c(1,2)],c(lat,lon),r=3393169),pixl.df[i,5], nearestpixl))
}
```

```{r}
# Create a new dataframe with the LIBS metatdata as well as features corresponding to each PIXL Abrasion
libstargets.df<-cbind(libstargets.df,"Distance"=0,"Bellegrade"=0,"Dourbes"=0,"Quartier"=0,"Alfalfa"=0,"ThorntonGap"=0,"BerryHollow"=0,"Novarupta"=0,"UganikIsland"=0)
```

```{r}
#DistCosine(pixl.df[1,c(1,2)],libstargets.df[1,c(2,3)], r=3393169)
#Calculate the distance between each LIBS target and each PIXL abrasion, then fill in the minimum distances
for(i in 1:nrow(libstargets.df)) {
libstargets.df[i,c(6:13)]<-c(distHaversine(pixl.df[1,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[2,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[3,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[4,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[5,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[6,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[7,c(1,2)],libstargets.df[i,c(2,3)],r=3393169),
distHaversine(pixl.df[8,c(1,2)],libstargets.df[i,c(2,3)],r=3393169))
libstargets.df[i,c(7:14)]<-c(distHaversine(pixl.df[1,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[2,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[3,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[4,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[5,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[6,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[7,c(1,2)],libstargets.df[i,c(3,4)],r=3393169),
distHaversine(pixl.df[8,c(1,2)],libstargets.df[i,c(3,4)],r=3393169))
libstargets.df[i,1]<-which.min(libstargets.df[i,c(6:13)])
libstargets.df[i,5]<-min(libstargets.df[i,c(6:13)])
libstargets.df[i,1]<-which.min(libstargets.df[i,c(7:14)])
libstargets.df[i,6]<-min(libstargets.df[i,c(7:14)])
}
libstargets.df$nearestpixl<-as.factor(libstargets.df$nearestpixl)
Expand All @@ -156,6 +144,7 @@ levels(libstargets.df$nearestpixl)<-(c("Bellegrade","Dourbes","Quartier","Alfalf


```{r}
#Create vectors of LIBS targets corresponding to each PIXL Abrasion
Bellegrade<-libstargets.df[libstargets.df$nearestpixl=="Bellegrade",]$target
Dourbes<-libstargets.df[libstargets.df$nearestpixl=="Dourbes",]$target
Quartier<-libstargets.df[libstargets.df$nearestpixl=="Quartier",]$target
Expand All @@ -166,82 +155,89 @@ Novarupta<-libstargets.df[libstargets.df$nearestpixl=="Novarupta",]$target
UganikIsland<-libstargets.df[libstargets.df$nearestpixl=="UganikIsland",]$target
```


### Ternary Diagram
```{r}
meters=100
included.libs<-(libstargets.df%>%
filter(Distance<meters))$target
libs.matrix <-libs.df %>%
filter(target %in% included.libs)
libs.matrix <- libs.matrix[,c(5,7:14)]
libs.tern <- as.data.frame(libs.matrix) %>%
mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>%
select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O,TiO2))
#Add the nearest Abrasion to the LIBS data
libs.pixl.merged <- cbind("Abrasion"=0,libs.df)
libs.tern<-cbind("Abrasion"=0,libs.tern)
libs.tern<-libs.tern%>%
libs.pixl.merged<-libs.pixl.merged%>%
mutate(Abrasion = ifelse(target%in%Alfalfa,"Alfalfa",
ifelse(target %in% Bellegrade, "Belegrade",
ifelse(target %in% BerryHollow, "BerryHollow",
ifelse(target %in% Dourbes, "Dourbes",
ifelse(target %in% Novarupta, "Novarupta",
ifelse(target %in% Quartier, "Quartier",
ifelse(target %in% ThorntonGap, "ThorntonGap",
ifelse(target %in% UganikIsland, "UganikIsland",Abrasion)))))))))
ifelse(target %in% UganikIsland, "Uganik Island",Abrasion)))))))))
targetdistance<-libstargets.df[,2:6]
libs.pixl.merged<-merge(libs.pixl.merged,targetdistance,by=c("target","sol","lat","lon"),all.x=T)
```

```{r}
#Add back some pixl features
pix<-pixl.df[,c(1,2,4,5)]
libs.pixl <- merge(libs.pixl.merged, pix, by.x="Abrasion",by.y="abrasion",all.x=TRUE)
#rename and reorder columns
libs.pixl<-cbind("LIBS.Target"=libs.pixl$target,libs.pixl[,4:5],"LIBS.Sol"=libs.pixl$sol,"LIBS.Point"=libs.pixl$point,"Distance"=libs.pixl$Distance,"PIXL.Abrasion"=libs.pixl$Abrasion,libs.pixl[,16:18],libs.pixl[,7:14])
colnames(libs.pixl)<-c("LIBS.Target","LIBS.Lat","LIBS.Lon","LIBS.Sol","LIBS.Point", "Distance","PIXL.Abrasion","PIXL.Lat","PIXL.Lon","PIXL.Campaign","LIBS.SiO2","LIBS.TiO2","LIBS.Al2O3","LIBS.FeOT","LIBS.MgO","LIBS.CaO","LIBS.Na2O","LIBS.K2O")
```

kabledf<-rbind("Distance (m)"=meters,"Targets"=length(included.libs),"Points"=nrow(libs.tern))
```{r}
#setwd("~/DAR-Mars-F24/StudentData")
#saveRDS(libs.pixl,"PIXL_LIBS_Combined.Rds")
```

The libs.pixl dataframe is now saved to PIXL_LIBS_Combined.Rds, which can be found in the StudentData folder.

### Ternary Diagram

Example of creating a ternary plot filtering the combined LIBS and PIXL data
```{r}
libs.tern <- libs.pixl %>%
mutate(x=(LIBS.SiO2+LIBS.Al2O3)/100,y=(LIBS.FeOT+LIBS.MgO)/100,z=(LIBS.CaO+LIBS.Na2O+LIBS.K2O)/100)
kable(kabledf)
libs.tern<-libs.tern[,c(6,7,19:21)]
```


```{r}
meters=7
ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) +
geom_point(data=libs.tern,aes(color=Abrasion,alpha=0.5)) +
geom_point(data=subset(libs.tern,Distance<=meters),aes(color=PIXL.Abrasion,alpha=0.5)) +
theme_rgbw() +
labs(title=paste("Mars LIBS Data Within",distance,"meters of PIXL",sep=" "),
labs(title=paste("Mars LIBS Data Within",meters,"meters of PIXL",sep=" "),
x="Si+Al",
y="Fe+Mg",
z="Ca+Na+K")+theme(legend.position="right") +
guides(alpha="none")
```

### Line plot

```{r}
meters=100
included.libs<-(libstargets.df%>%
filter(Distance<meters))$target
libs.matrix <-libs.df %>%
filter(target %in% included.libs)
ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) +
geom_point(data=subset(libs.tern,Distance<=meters),aes(color=PIXL.Abrasion,alpha=0.5)) +
theme_rgbw() +
labs(title=paste("Mars LIBS Data Within",meters,"meters of PIXL",sep=" "),
x="Si+Al",
y="Fe+Mg",
z="Ca+Na+K")+theme(legend.position="right") +
guides(alpha="none")
```

libs.matrix <- libs.matrix[,c(5,7:14)]

libs.matrix<-libs.matrix[,c(1:2,4:9,3)]
### Line plot

libs.matrix<-cbind("Abrasion"=0,libs.matrix)
Example of how to add the pixl data as extra rows to the LIBS data, so that it can be graphed on the same plot

libs.matrix<-libs.matrix%>%
mutate(Abrasion = ifelse(target%in%Alfalfa,"Alfalfa",
ifelse(target %in% Bellegrade, "Belegrade",
ifelse(target %in% BerryHollow, "BerryHollow",
ifelse(target %in% Dourbes, "Dourbes",
ifelse(target %in% Novarupta, "Novarupta",
ifelse(target %in% Quartier, "Quartier",
ifelse(target %in% ThorntonGap, "ThorntonGap",
ifelse(target %in% UganikIsland, "UganikIsland",Abrasion)))))))))
```{r}
#remove certain features from the combined pixl libs dataset
libs.matrix <- libs.pixl[,c(7,1,11,13:18,12)]
#rename columns
libs.matrix<-cbind(libsorpixl=1,libs.matrix)
colnames(libs.matrix)<-c("libsorpixl","Abrasion","targets+names","SiO2","Al2O3","FeOT","MgO","CaO","Na2O","K2O","TiO2")
```


Expand All @@ -262,9 +258,7 @@ pixl.df<-cbind(libsorpixl=0,pixl.df)
```

```{r}
#rename pixl columns and combine dataframes
colnames(pixl.df)<-colnames(libs.matrix)
pixllibs.df<-rbind(pixl.df,libs.matrix)
```