logo
down
shadow

Create a vector in a dataframe based on matching a second vector to a row name in another object


Create a vector in a dataframe based on matching a second vector to a row name in another object

By : Pete
Date : November 22 2020, 02:59 PM
should help you out I'd suggest two options
Either you can use base match function as in
code :
d$intercept <- m1.out[match(d$group, rownames(m1.out)), 1]
library(data.table) #v1.9.6+
setDT(m1.out, keep.rownames = TRUE, check.names = TRUE) 
setDT(d)[m1.out, intercept := i.X.Intercept., on = c(group = "rn")]
d
#    group    x   y   intercept
# 1:     a  1.1 1.0  0.02631376
# 2:     a  2.3 2.1  0.02631376
# 3:     a  1.9 2.1  0.02631376
# 4:     a  3.5 3.6  0.02631376
# 5:     b 11.1 1.2 10.07444484
# 6:     b 12.3 2.2 10.07444484
# 7:     b 11.9 1.8 10.07444484
# 8:     b 13.5 3.4 10.07444484


Share : facebook icon twitter icon
sort vector based on another partial matching vector

sort vector based on another partial matching vector


By : W. Herrgott
Date : March 29 2020, 07:55 AM
like below fixes the issue I have two character vectors A and B. Most of A has matching strings in B, matched by the first 6 characters. A strings always end with 'd', and B strings always ends with 'z'. I'd like to sort B based on A, and put any non-matches in C. , Try this:
code :
m <- match(substr(B,1,6), substr(A,1,6))
B[na.exclude(m)]
#[1] "ABCD01z" "DEFG10z" "ZYXW43z"
B[is.na(m)]
#[1] "DFGS88z"
Create vector by matching vector to a dataframe [R]

Create vector by matching vector to a dataframe [R]


By : Flavius
Date : March 29 2020, 07:55 AM
I wish this help you Here are some possibilities. They do not use any packages.
1) For each element of z compute the subset of rows in test with that zCode and then take the maximum of each x:
code :
data.frame(z, x = sapply(z, function(z) max(subset(test, z == zCode)$x)))
    z   x
1  53  98
2  57 100
3 110 114
merge(data.frame(z), aggregate(x ~ zCode, test, max), by = 1, all.x = TRUE)
    z   x
1  53  98
2  57 100
3 110 114
Lines <- "
  zCode  x
1  110 114
2  108 150
3   57 100
4   53  98
5  114  67
6  143 126
7  110  95
8  106 101
9  103  70
10 149  73"
test <- read.table(text = Lines)

z <- c(53, 57, 110)
Rename columns in a dataframe based on vector of names (character vector)

Rename columns in a dataframe based on vector of names (character vector)


By : mriss
Date : March 29 2020, 07:55 AM
This might help you If you are flexible about the tools to read the file with then tidyverse is a really useful library to work with:
code :
install.packages('tidyverse')
library('tidyverse')

read_fwf(file=fixedFileName, col_positions=fwf_widths(c(32,3,5,15,rep(1, 225)), 
    col_names = c("ID", "Gender", "Score", "Department", VNames)), col_types = NULL,
    trim_ws = TRUE)
?tidyverse
?read_fwf
?fwf_widths
etc
How do I efficiently change the values of a vector in a dataframe based off of a current vector in r?

How do I efficiently change the values of a vector in a dataframe based off of a current vector in r?


By : user3049551
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to create a new vector "seasons" based off of the months vector already provided in my data. I am using the built-in txhousing dataset; I have already filtered the dataframe to only include information on the city of Houston and have called this new dataframe houston. , dplyr::case_when provides a clean coding for this.
code :
library(dplyr)

# Reprex dataframe (always include one in your questions)
houston <- tibble(month = 1:12)

houston %>%
  mutate(seasons = case_when(month %in% c(1:2, 12) ~ "winter",
                             month %in% 3:5        ~ "spring",
                             month %in% 6:9        ~ "summer",
                             month %in% 10:11      ~ "fall"))

# A tibble: 12 x 2
   month seasons
   <int> <chr>  
 1     1 winter 
 2     2 winter 
 3     3 spring 
 4     4 spring 
 5     5 spring 
 6     6 summer 
 7     7 summer 
 8     8 summer 
 9     9 summer 
10    10 fall   
11    11 fall   
12    12 winter
Conditional replacement in vector based on the another character vector in dataframe

Conditional replacement in vector based on the another character vector in dataframe


By : Subham Mishra
Date : March 29 2020, 07:55 AM
To fix this issue I have a dataframe with column that called "mutation". They could be SNP like "C > A", insertion like "+TTTAAG" or deletion like "-CTTGA". For example: , Solution using grep and ifelse:
code :
genotype <- data.frame(position = 1:3,
                       mutation = c("C > A", "+TGCA", "-ACGT"))
genotype$mutation_type <- 
    ifelse(grepl("\\+", genotype$mutation), "Insertion", 
           ifelse(grepl("\\-", genotype$mutation), "Deletion", "SNP"))

  position mutation mutation_type
1        1    C > A           SNP
2        2    +TGCA     Insertion
3        3    -ACGT      Deletion
Related Posts Related Posts :
  • How to sort groups within sorted groups?
  • How to get randomForest model output in probability using Caret?
  • how to plot a data frame into R
  • Calculate mean of a range of rows
  • How to sum the values in a text file?
  • Looking up polygons in a shapefile that point belong in... why does it work with some shapefiles, not with others?
  • R creating a list using variable columns from df
  • Determine programmatically if a function call refers to your own function or a package's (or base R's) function?
  • Adding Special Characters to a plot in R
  • How to dynamically change R .libPaths() based on hostname?
  • Print arguments of a function in R
  • Creating Variables via A loop in r
  • How to randomly divide an integer into a fixed number of integers, such that the obtained tuples are uniformly distribut
  • Remove columns using column name based on levels of factors
  • What units are the 'width = ' in geom_bar(aes = ) and position_dodge(width = ) rendered in?
  • Assigning Dates to Fiscal Year
  • conditional removal from data frame
  • Getting variables out of a function in R
  • Bar colors in ggplot2 geom_bar not applied correctly (R)
  • Convert julian day to mm/dd and add year from another column in dataframe in r
  • Change SMOTE parameters inside CARET k-fold cross-validation classification
  • rollapply classes each segment the same
  • rename the header of the spliting group in r
  • Summary stats by factor level for multiple variables
  • Processing Rmarkdown inside chunks
  • R: how to join the duplicate rows in one dataframe
  • Creating a unified time-series, with dates coming from different (natural) languages
  • R error: attempt to apply non-function , randomForest , rfImpute
  • assign multiple color to each vertex in igraph
  • How do I add shading and color to the confidence intervals in ggplot 2 generated Kaplan-Meier plot?
  • How to generate spatial points with a pattern
  • ggplot2 + stat_contour variable binwidth
  • Add fake data to a data frame based on variable condition
  • Utilizing a character vector inside a function
  • match fundction with data frames that are differently constructed
  • Overlaying plots with a horizontal date in R
  • lapply'ing a for loop returns NULL
  • Dynamically add UI elements and gather their input in a dataframe in shiny
  • All the possible outcomes
  • How to Use corrplot with method="number" and Drop Leading Zero?
  • cbind converting factor to numeric
  • create names of loop when iterating data frame
  • R reading multiple raster files gives error in for loop
  • Debugging into and changing R-functions in packages
  • Categorical variables in R - which one does R pick as reference?
  • Create new data frame based on values from another data frame
  • Subplots using Plotly in R (bug fixed)
  • WinBUGS error 'expected key word END' caused by wrong exponential code (not length of data)
  • Predict with gls
  • R list to wide (sparse) data frame
  • Installing ZeligMultilevel. Error : object ‘describe’ is not exported by 'namespace:Zelig'
  • How to use a variable created in a function in your console
  • Dynamic grid plots in shiny R
  • Display unicode in R
  • value of NULL after inserting value
  • Model multiple imputation with interaction terms
  • Dynamically Splitting and naming the dataframe
  • Cross-correlation of 5 time series (distance) and interpretation
  • Grouping columns based on pairwise equality
  • Checking if multiple variables are all the same value in R
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co