C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# How to copy a row from a data frame to a matrix in R?

By : Amer Aljarallah
Date : November 21 2020, 11:01 PM
I hope this helps you . You should be able to assign the values after unlisting f[1,], since f[1,] returns a one row data frame which is a list, m[1,] returns an atomic vector and this answers the question Why is dim(m[1,]) "NULL" instead of "1 3"? because a 1D atomic vector doesn't have the dimension attribute. Trying to assign a list to an atomic vector causes problem:
code :
``````m = matrix(0, nrow=2, ncol=3)
f = data.frame(x = 1:3, y = 2:4, z = 3:5)
m
#     [,1] [,2] [,3]
#[1,]    0    0    0
#[2,]    0    0    0
m[1,] = unlist(f[1,])
m
#     [,1] [,2] [,3]
#[1,]    1    2    3
#[2,]    0    0    0

class(m[1,])
#[1] "numeric"
class(f[1,])
#[1] "data.frame"
``````

Share :

## R Modifying a region of a matrix/data frame depending on another matrix/data frame

By : user3557876
Date : March 29 2020, 07:55 AM
this will help I have a question about modifying a matrix. I have managed to change values in a matrix depending on the value of another matrix when they are of the same dimension. But now I need to apply this procedure to matrixes with different dimensions. In other words, I want to apply some changes to a "region" of the bigger matrix depending on the values of the smaller one, bearing in mind that I know the positions of the smaller matrix associated to the bigger one. , Just select the submatrix and do what you want, i.e.:
code :
``````A[1:5,2:8][B == 1] = 0
``````
``````A[row:nrow(A),col:ncol(A)][B == 1] = 0
``````
``````A = matrix(rep(1:8, each = 5), nrow = 5)
B = matrix(c(rep(0, 7), 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, rep(0, 8)), nrow = 5, byrow = TRUE)
A[1:5,2:8][B == 1] = 0
``````

## Place individual items from a data.frame/matrix into a new data.frame organized by groups

By : Tharindu Hettiarachc
Date : March 29 2020, 07:55 AM
it helps some times Well, a data.frame seems like an incorrect data structure to store the results. Data.frames expect rectangular data where each row has data for each of the columns. A list seems like a better choice in this case. I would create a named reassignment vector like so
code :
``````reassign<-c(Ac="Red",aB="Blue",Ba="Blue", Bb="Blue",
CC="Yellow",bC="Yellow", AB="Purple", ac="Orange")
``````
``````split(temp1, reassign[temp1])
``````
``````\$Blue
[1] "aB" "Ba" "Bb"

\$Orange
[1] "ac"

\$Purple
[1] "AB"

\$Red
[1] "Ac"

\$Yellow
[1] "CC" "bC"
``````

## Using set() from data.table package to copy and paste values from a data frame to another, within a loop of data frame c

By : user10549
Date : March 29 2020, 07:55 AM
Does that help I'm not quite sure why (i suspect assign), but it seems that your data.frame (A_id, B_id...) are linked in are not different, they are just different names pointing to the same object in RAM.
A work around is to use data.table::copy to make a copy in RAM of the object.
code :
``````for (i in 1:length(letters)){
assign(paste0(letters[i],"_id"), copy(basedata))
set(get(paste0(letters[i],"_id")), NULL,j = 1L, value = Values_df[,i]) #PROBLEM
}
``````

## How to convert matrix to a data frame with the column and row names from the original data frame

By : Joy Kulkanik Nontach
Date : March 29 2020, 07:55 AM
should help you out I have the following original data frame: , We can use the dimnames assignment as these are both matrixes
code :
``````dimnames(transformed_mat) <- dimnames(original_df)
transformed_mat
#                A             B             C
#foo -2.961008e-06 -9.940173e-07 -2.227767e-06
#bar  1.681692e-05  7.630277e-04  2.943174e-05
#qux -1.268318e-04 -1.033156e-04 -1.906606e-04
``````
``````attr(transformed_mat, "dimnames") <- attr(original_df, "dimnames")
``````

## How to create a large data frame in R with or without creating a matrix first and then converting it to a data.frame?

By : Serhii Rusov
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You could construct an empty data frame of size 80,000 x 80,000 as follows: