characters to doubles R - r

I create a frame from different vectors
pat1<-c(11, 12, 13, 14, 15)
pat2<-c(1:5)
pat3<-seq(1,10, by=2)
pat4<-seq(-5,3, by=2)
pat5<-c(pat1+pat2)
variables<-c("a","b","c","d","e")
mydata<-data.frame(variables, pat1, pat2,pat3, pat4, pat5)
mydata<-t(mydata)
I translate my columns to rows and I get the correct table, but numbers are not doubles
[,1] [,2] [,3] [,4] [,5]
variables "a" "b" "c" "d" "e"
pat1 "11" "12" "13" "14" "15"
pat2 "1" "2" "3" "4" "5"
pat3 "1" "3" "5" "7" "9"
pat4 "-5" "-3" "-1" " 1" " 3"
pat5 "12" "14" "16" "18" "20"
How shall I get doubles for my pat values?

Related

Change row.names from num to chr

I have a data.frame in R and the row.names are a character and I would like them to be numeric. I've tried to find the same issue like here but it doesn't work.
Here is my code:
attr(DF1, "row.names")
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"
after I do what I linked above:
DF1$id <- as.integer(row.names(DF1))
DF1[order(DF1$id), ]
I get the same result:
attr(DF1, "row.names")
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"
and I would like the result to be as in with dataframe D2:
attr(DF2, "row.names")
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
From the help page ?rownames it says (emphasis mine)
For a data frame, value for rownames should be a character vector of non-duplicated and non-missing names (this is enforced), and for colnames a character vector of (preferably) unique syntactically-valid names. In both cases, value will be coerced by as.character, and setting colnames will convert the row names to character.
You could make them an integer like this.
df <- data.frame(x = 1:3)
rownames(df) <- as.character(5:7)
attr(df, "row.names")
#> [1] "5" "6" "7"
rownames(df) <- as.integer(rownames(df))
attr(df, "row.names")
#> [1] 5 6 7
Note that row.names will always return a character vector. See ?row.names.
row.names(df)
#> [1] "5" "6" "7"

for loop iteration over multiple subarrays

I have the variable assignments
N <- 10
H <- 10
K <- 2 # number of subarrays
perms <- 10
I set up an empty array as follows:
pop <- array(dim = c(c(perms, N), K))
Then I assign character labels:
haps <- as.character(1:H)
Now, I assign probabilities:
probs <- rep(1/H, H)
I then create a 'for' loop:
for(j in 1:perms){
for(i in 1:K){
pop[i,j] <- sample(haps, size = N, replace = TRUE, prob = probs)
}
}
'pop' should now contain character labels from 1:H across both subarrays. Instead, I end up with an error "incorrect number of subscripts on matrix."
I am not sure why R is producing the error.
Any assistance is appreciated.
You gave 2 subscripts to the 3 dimensional vector pop. Also the dimensions of the sample wouldn't have matched the dimensions of the slice of pop even if you had added a 3rd subscript because the order was wrong.
dim(pop)
[1] 10 10 2
You may have wanted this:
for(j in 1:perms){
for(i in 1:K){
pop[1:10,j,i] <- sample(haps, size = N, replace = TRUE, prob = probs)
}
}
pop
, , 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "6" "8" "8" "6" "3" "3" "1" "9" "7" "8"
[2,] "7" "10" "5" "3" "7" "10" "7" "1" "5" "8"
[3,] "9" "7" "1" "4" "1" "3" "2" "7" "6" "5"
[4,] "6" "8" "9" "4" "9" "7" "10" "9" "7" "2"
[5,] "1" "3" "2" "6" "10" "3" "3" "9" "10" "6"
[6,] "9" "3" "8" "1" "6" "6" "4" "8" "8" "9"
[7,] "9" "2" "2" "2" "3" "9" "8" "6" "10" "10"
[8,] "9" "5" "3" "8" "3" "4" "1" "6" "8" "4"
[9,] "10" "8" "1" "3" "10" "2" "5" "10" "6" "4"
[10,] "2" "1" "8" "10" "5" "5" "7" "8" "7" "6"
, , 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "1" "10" "10" "7" "3" "6" "3" "2" "4" "1"
[2,] "8" "5" "10" "8" "3" "6" "6" "6" "8" "2"
[3,] "1" "6" "1" "5" "6" "3" "6" "1" "7" "9"
[4,] "7" "10" "5" "5" "7" "5" "3" "3" "10" "1"
[5,] "7" "3" "6" "8" "3" "9" "6" "2" "7" "3"
[6,] "1" "9" "4" "9" "1" "1" "10" "4" "3" "9"
[7,] "9" "3" "10" "1" "2" "2" "2" "3" "5" "4"
[8,] "8" "10" "8" "6" "9" "6" "9" "9" "2" "4"
[9,] "4" "6" "1" "1" "6" "5" "6" "6" "10" "3"
[10,] "5" "9" "9" "7" "9" "6" "4" "2" "10" "9"
Though it seems strange to me that you want the numbers in haps and therefore pop to be characters, but I assume that you have your reasons.
This is a column-wise assignment of the samples. I'm not sure if it matters, but if you prefer row-wise then you can do the index like this:
pop[j,1:10,i]

Calculate the difference between rows [duplicate]

I want to take an existing MxN matrix and create a new M-1xN matrix such that for each columns, the elements are the difference between adjacent row elements of the original matrix.
The idea is the data goes from a cumulative type to a rate type...
eg:
I have (where each column is a specific data series).
[,1] [,2] [,3]
[1,] "17" "16" "15"
[2,] "34" "32" "32"
[3,] "53" "47" "48"
[4,] "72" "62" "63"
[5,] "90" "78" "79"
[6,] "109" "94" "96"
I would like -
[,1] [,2] [,3]
[1,] "17" "16" "17"
[2,] "19" "15" "16"
[3,] "19" "15" "15"
[4,] "18" "16" "16"
[5,] "19" "16" "17"
It's very simple for numerical data (not sure why you have characters):
diff(m)
With the character data, this should work:
diff(matrix(as.numeric(m), dim(m)))
It's a bit strange with the character format, but here's a way:
# Set up the data
mymat<-matrix(c("17","16","15",
"34","32","32",
"53","47","48" ,
"72","62","63",
"90","78","79" ,
"109","94","96"),nrow=6,byrow=TRUE)
Use the apply function with an anonymous function centered around diff.
apply(mymat, 2, function(x)as.character(diff(as.numeric(x))))
# [,1] [,2] [,3]
# [1,] "17" "16" "17"
# [2,] "19" "15" "16"
# [3,] "19" "15" "15"
# [4,] "18" "16" "16"
# [5,] "19" "16" "17"
If the data are numeric to begin with and a numeric result is desired, then the above could be simplified to
apply(mymat, 2, diff)
In case you want to subtract the columns of a matrix (and not the rows), try:
col.diff = t(diff(t(mat)))

Combine selected columns from many matrices

I am trying to accomplish the following task to get to matrix d:
d1<-matrix(as.factor(rep(sample(1:10,10,T),5)),ncol=5)
d2<-matrix(as.factor(rep(sample(1:10,10,T),5)),ncol=5)
d3<-matrix(as.factor(rep(sample(1:10,10,T),5)),ncol=5)
d<-cbind(
cbind(d1[,2],d1[,5]),
cbind(d2[,2],d2[,5]),
cbind(d3[,2],d3[,5])
)
But for many matrices d1...dn, say.
More generally I would like to select the same column numbers from a series of matrices and append into a single matrix. The focus of this task is on combining, not creating the matrices. The factor-type column vectors should be preserved.
I thought about something along the lines of
d<-matrix(nrow=10)
dl<-list(d1,d2,d3)
for (i in 1:3){
d<-cbind(d,dl[[i]][,2],dl[[i]][,5])
}
But maybe there is a better way.
You can create a list of your matrices and use do.call and lapply to get what you want:
matList <- list(d1, d2, d3)
do.call(cbind, lapply(matList, function(x) x[, c(2, 5)]))
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "3" "3" "3" "3" "10" "10"
# [2,] "4" "4" "2" "2" "3" "3"
# [3,] "6" "6" "7" "7" "7" "7"
# [4,] "10" "10" "4" "4" "2" "2"
# [5,] "3" "3" "8" "8" "3" "3"
# [6,] "9" "9" "5" "5" "4" "4"
# [7,] "10" "10" "8" "8" "1" "1"
# [8,] "7" "7" "10" "10" "4" "4"
# [9,] "7" "7" "4" "4" "9" "9"
# [10,] "1" "1" "8" "8" "4" "4"
By the way, the data type in your matrix is character, not factor. See the help page at ?matrix where you will find the following:
The method for data frames will return a character matrix if there is only atomic columns and any non-(numeric/logical/complex) column, applying as.vector to factors and format to other non-character columns.

New matrix containing difference between row elements of original

I want to take an existing MxN matrix and create a new M-1xN matrix such that for each columns, the elements are the difference between adjacent row elements of the original matrix.
The idea is the data goes from a cumulative type to a rate type...
eg:
I have (where each column is a specific data series).
[,1] [,2] [,3]
[1,] "17" "16" "15"
[2,] "34" "32" "32"
[3,] "53" "47" "48"
[4,] "72" "62" "63"
[5,] "90" "78" "79"
[6,] "109" "94" "96"
I would like -
[,1] [,2] [,3]
[1,] "17" "16" "17"
[2,] "19" "15" "16"
[3,] "19" "15" "15"
[4,] "18" "16" "16"
[5,] "19" "16" "17"
It's very simple for numerical data (not sure why you have characters):
diff(m)
With the character data, this should work:
diff(matrix(as.numeric(m), dim(m)))
It's a bit strange with the character format, but here's a way:
# Set up the data
mymat<-matrix(c("17","16","15",
"34","32","32",
"53","47","48" ,
"72","62","63",
"90","78","79" ,
"109","94","96"),nrow=6,byrow=TRUE)
Use the apply function with an anonymous function centered around diff.
apply(mymat, 2, function(x)as.character(diff(as.numeric(x))))
# [,1] [,2] [,3]
# [1,] "17" "16" "17"
# [2,] "19" "15" "16"
# [3,] "19" "15" "15"
# [4,] "18" "16" "16"
# [5,] "19" "16" "17"
If the data are numeric to begin with and a numeric result is desired, then the above could be simplified to
apply(mymat, 2, diff)
In case you want to subtract the columns of a matrix (and not the rows), try:
col.diff = t(diff(t(mat)))

Resources