logo
down
shadow

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


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 : facebook icon twitter icon
R Modifying a region of a matrix/data frame depending on another matrix/data frame

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

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

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

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?

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:
Related Posts Related Posts :
  • Building a symmetric binary matrix
  • Change color and add shape to lines on ggplot2 geom_freqpoly
  • Count event occurrence and assign it to individuals according to date and place of interview
  • ggplot2 is mutating/transforming size variables -- how to get back original data?
  • How to output literal backticks in knitr::spin
  • geom_text how to position the text on bar as I want?
  • How to escape the symbol of percentage '%' in r?
  • How to get R plot to plot variable on heat.color scale
  • Dynamically selecting principal components from the PCA output
  • Replicate a regression using a random subset of data each time and check distribution of regression coefficients?
  • How to find a string in a vector in r?
  • How to replace outliers with NA having a particular range of values in R?
  • rearrange data: convert from water year to calendar year
  • Plot multiple graphs on one plot in R within loop
  • Unconditional Probability Calculation in R
  • Calculate & add column length_of_time from date column in R
  • Asterisk in ggplot's annotate()
  • Importing a similar column (eg. pH column) from 60 csv files thru use of loop and making a matrix .
  • Reading text file with multi-char delimiter to a list in R
  • ggplot2 scale colours for heatmap
  • Looking to save a 3D spectrograph generated by SeeWave as .STL or similar in R
  • Separating three dimensional array by the stratifying variable
  • "Object '...' not found when referenced object is a nested function in R
  • How to correctly clearShapes in Leaflet in a Shiny app
  • NetLogo 5.3.1 and R 3.3.1 on Mac 10.10.6 headless exception
  • Extract unique values from column in dataframe, grouped by ID
  • Stacked Bar Chat (ggplot) in R:: Trying to create more than one bar in the chart
  • R: Creating a function to merge two columns
  • Error in knearest(Darr, P_set, K) : object 'knD' not found
  • Determining least significant predictor in Linear Regression Model with R
  • listcolumns and multidplyr
  • How to check for the presence of a location in a sentence in R?
  • Using mapply over a matrix when the function makes use of elemets' location in the matrix
  • R - stratified sampling for Person Period file
  • Merging two xts to create NAs at certain time stamps
  • How to subset data in a loop in R?
  • How to use sprintf to show string "\t"?
  • Subset Columns based on partial matching of column names in the same data frame
  • R Extract number from string
  • How to put 2 boxplot in one graph in R without additional libraries?
  • ggplot omits polygon holes
  • ggplot2 geom_boxplot side by side with facets
  • outlier labels incorrectly assigned with ggplot2 box plot
  • capture column pattern frequency
  • R remove tags starting with U
  • R, dplyr and snow: how to parallelize functions which use dplyr
  • R: Separate String Without Delimiter (like Fixed Width in Excel)
  • In R, is there a parsimonious or efficient way to get a regression prediction holding all covariates at their means?
  • Determine mode locations of the kernel density estimate of multimodal univariate data
  • Splitting a column ( column A with numbers) into sub columns with columns names as
  • Which calculation of 2 to the 168th power is correct, and why?
  • 403 Error When Using Rvest to Log Into Website For Scraping
  • R ggplot2 doesn't know a custom function
  • Shiny - Where can I find ObserveEvent
  • decreasing coefficients in R's coefplot?
  • Summing nearby elements of a matrix in R
  • Unequal number of elements when webscraping
  • Reshape data into long columns based on variable in R for IRR
  • ARIMA modelling, prediction and plotting with CO2 dataset in R
  • modelr: cross-validated model fitting for all variables in a dataset that match criterion
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com