AFNI Class Notes: 10/25/2019

You can open imaging data in R using several packages such as:

You will want to convert to nifti format for the first 2 if you have data in afni format (BRIK/HEAD) using 3dAFNItoNIFTI

 Quick walk through R

# see libraries
library()
# install library from cran
# if on shared server best to request from IT
    install.packages('psych')
# use library
library(psych)
# invesitgate library search all
??psych
# see some stuff in the library psych enter this and tab
psych::
# see help
?describe
# or see ?help for more options
help(describe)
# or simple version
args(describe)
# where am I?
getwd()
# where am I going?
setwd('Powers_extractions/')
# did I get there
getwd()
# how did I get there
history()
# how to pull data into R
csv=read.table('0001R_restPrep_Powers.csv',header=T)
# what did I bring in?
class(csv)
# assign a numeric
t=1
class(t)
# assign a character
t="alan"
class(t)
# remove it and verify
rm(t)
ls()
# other ways to check what I brought in
str(csv)
csv
dim(csv)
head(csv)
head(csv,1)
csv[1:5,1:5]
# plot the data
plot.ts(csv[,3:8])
matplot(csv[,3:8],type='l')
hist(csv[,3])
# get some stats
describe(csv[,1:20])
mean(csv[,3])
# do that in a loop
for(i in 3:dim(csv)[2]){print(mean(csv[,i]))}
# read in simple vector
rl=readLines('0001RRESTBP_SDCensor')
class(csv)
length(rl)
# create matrix and vector
matrix(0,10,10)
data.frame(matrix(0,10,10))
# dataframes can have named cols and rows
names(df)
colnames(df)
rownames(df)
df$X1
# but the data looks the same
df[,1]
m[,1]
# data.frames can add character columns
df$X11=c("A","B","C","D","E","F","G","H","I","L")
str(df)
# but a matrix needs all types the same
m[,10]=c("A","B","C","D","E","F","G","H","I","L")
m

# with a library you can read niftis
library('ANTsR')
t1=antsImageRead('0001R.nii.gz')
# see the dimensions and info
dim(t1)
t1
str(t1)
# plot it
plot(t1)
?plot.antsImage
hist(t1)
# get stats
mean(t1)
# another imaging library
library(oro.nifti)
# which can read
t1o=readNIfTI('0001R.nii.gz')
# and convert to matrix
t1m=t1o@.Data