logo
down
shadow

Creating columns based on total number of columns in a data frame R


Creating columns based on total number of columns in a data frame R

Content Index :

Creating columns based on total number of columns in a data frame R
Tag : r , By : afarouk
Date : November 26 2020, 04:01 AM

I hope this helps . A possible, quick solution would be something like the following. After specifying the working directory, you read the data
data <- read.csv("filename.csv")
if (ncol(data) < 13) {
new_df <- as.data.frame( matrix(NA, nrow = nrow(data), ncol = 13 - ncol(data)) )
df <- cbind(data, new_df)
}
colnames(df) <- c("Fac", "m.Jan", "m.Feb", "m.Mar", "m.Apr", "m.May", "m.Jun",
                    "m.Jul", "m.Aug","m.Sep", "m.Oct", "m.Nov", "m.Dec")

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Collapse data frame into single row and creating new columns based on row R


Tag : r , By : user122937
Date : March 29 2020, 07:55 AM
this will help I have a data frame with object names and a list of statistical moments for that object, like this: , You could do:
 df1$id <- 1
 reshape(df1, idvar="id", timevar="Object", direction="wide")[-1]
  #  Mean.x IQR.x Skew.x Mean.y IQR.y Skew.y Mean.z IQR.z Skew.z
  #1      1     1      1      2     2      2      3     3      3
 library(reshape2)
 dcast(melt(df1, id.var=c('id', 'Object')), id~..., value.var='value')[-1]
 #   x_Mean x_IQR x_Skew y_Mean y_IQR y_Skew z_Mean z_IQR z_Skew
 #1      1     1      1      2     2      2      3     3      3
 library(dplyr)
 library(tidyr)

  df1 %>% 
      gather(Var, Val, Mean:Skew) %>% 
      unite(VarNew,Object, Var, sep="") %>%
      spread(VarNew, Val) %>%
      select(-id)
  #    xIQR xMean xSkew yIQR yMean ySkew zIQR zMean zSkew
  #1    1     1     1    2     2     2    3     3     3
df1 <- structure(list(Object = c("x", "y", "z"), Mean = 1:3, IQR = 1:3, 
 Skew = 1:3), .Names = c("Object", "Mean", "IQR", "Skew"), class = "data.frame", row.names = c(NA, 
-3L))

Creating a new column based on two columns in a data frame


Tag : r , By : Andrew
Date : March 29 2020, 07:55 AM
Does that help If you use the row number of the grouping it should get you what you want.
library(dplyr)

dat <- data.frame(IndID = factor(c(1,1,2,2,2,3,3)),
                  Order = c(1,2,1,2,3,1,2),
                  Media = factor(c("A", "B", "B","A","B","A", "A")))

res <- dat %>%
  group_by(IndID, Media) %>%
  mutate(count = row_number(),
         WantThis = paste(Media,count,sep=""))

#Source: local data frame [7 x 5]
#Groups: IndID, Media [5]
#
#   IndID Order  Media count WantThis
#  (fctr) (dbl) (fctr) (int)    (chr)
#1      1     1      A     1       A1
#2      1     2      B     1       B1
#3      2     1      B     1       B1
#4      2     2      A     1       A1
#5      2     3      B     2       B2
#6      3     1      A     1       A1
#7      3     2      A     2       A2

Matching values from multiple columns in 1 data frame to key in second data frame and creating columns


Tag : r , By : Ryuken
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have 2 data frames. One (df1) looks like this: , Here's a tidyverse solution. First, I define the data frames.
df1 <- read.table(text = " var.1 var.2 var.3 var.4
                  1     7     9     1     2
                  2     4     6     9     7
                  3     2     NA    NA    NA", header = TRUE)


df2 <- read.table(text = "  var.a var.b var.c var.d
1     1     b     c     d
2     2     f     g     h
3     4     j     k     l
4     7     j     k     z", header=TRUE)
# Load libraries
library(tidyr)
library(dplyr)
library(tibble)
# Manipulate data
df1 %>% 
  rownames_to_column() %>% 
  gather(variable, value, -rowname) %>% 
  left_join(df2, by = c("value" = "var.a")) %>% 
  gather(foo, bar, -variable, -rowname) %>% 
  unite(goop, variable, foo) %>% 
  spread(goop, bar) %>% 
  select(-rowname)
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
#>   var.1_value var.1_var.b var.1_var.c var.1_var.d var.2_value var.2_var.b
#> 1           7           j           k           z           9        <NA>
#> 2           4           j           k           l           6        <NA>
#> 3           2           f           g           h        <NA>        <NA>
#>   var.2_var.c var.2_var.d var.3_value var.3_var.b var.3_var.c var.3_var.d
#> 1        <NA>        <NA>           1           b           c           d
#> 2        <NA>        <NA>           9        <NA>        <NA>        <NA>
#> 3        <NA>        <NA>        <NA>        <NA>        <NA>        <NA>
#>   var.4_value var.4_var.b var.4_var.c var.4_var.d
#> 1           2           f           g           h
#> 2           7           j           k           z
#> 3        <NA>        <NA>        <NA>        <NA>

How to transform the content of a two-columns data frame based on row number of another data frame


Tag : r , By : user149634
Date : March 29 2020, 07:55 AM
Hope that helps I have two data frames. One is a data frame that only contains names , You can use:
sapply(df1, match, df2$name)

     name1 name2
[1,]     1     2
[2,]     5     3
[3,]     5     4
library(purrr)

map_df(df1, match, df2$name)

# A tibble: 3 x 2
  name1 name2
  <int> <int>
1     1     2
2     5     3
3     5     4

Creating a new column based on two old columns in a data frame


Tag : r , By : Ryuken
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • How to change the position of stacked stacked bar chart in ggplot in R?
  • How to divide each of a range a variables by a second range of variables in R
  • Why do I need to assemble vector before scaling in Spark?
  • How to select individuals which appear in multiple groups?
  • How can I fill columns based on values in another column?
  • 32 bit R and 64 bit R: output differs
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com