Change all cells marked X into cell value shown in column P - formula

I need an easy way to convert all X's in a column into the value shown in a cell.
Basically we want to sell multiple products to a client with a target order value split amongst the relevant products - I have done a CountA formula to show how many columns are not blank. Then I did a simple divide to divide the total value over the columns that are not blank (if there are 2 columns marked X then it would be 10,000 / 2 - assuming the target value is 10k) Now I need to change all the X's into the figure shown in the cell as shown in the pic.
I cant for the life of me think of an easy way of doing it but sureley there is?
Screen shot of sheet

You need 2 sets of columns. Your first set has the X's and the blanks to mark which categories are applicable. The second set has the calculated values for the selected categories.
In the value columns, you can use a formula like the following for the first data row and first category:
=IF(E2 = "x", $D2, 0)
Assuming "D" is your "Dvided total" column, and "E" is your "Dairy & S..." column, and "2" is your first data row.

Related

How to add specific row into data.table?

I have a problem with data table in r. I have created a data table (approx. 1x60 columns) by using "sample" function. (eg column 1 <- sample(data, 1) and so on. I have sampled over two (yes/no) or five values (a/b/c/d/e). So I ended up with 1 row data table with 60 columns, each column contain 'yes', 'no', and a/b/c/d or e value. The problem is that I have to have all combinations. I have tried 'expand grid' function, however, I stuck with 1 million rows, so I have to have more control. Is there any possibility to add another empty row to existing data table and fill that row with the remaining possibilities, then add third row and repeat? I mean: if in 1st column there is 'yes' value in the 1st row, there should be 'no' value in the 2nd row and so on. Please let me know if you have any idea what function could I use. I have spend many hours looking for some answer. Thanks a lot for your help.

R: stacking up values from rows of a data frame

I started programming in R yesterday (literally), and I am having the following issue:
-I have a data frame containing R rows, and each row contains N values.
Rows are identified by the first and second field, while the other N-2 are just numerical values or NA.
-Some rows have identical first field and identical second field, something like:
row 1: a,b, third_field, .. ,last_field
row 2: a,b, third_field, .. ,last_field
the rule is that usually the first line will have its fields containing some numbers and some NA, while the second row will contain NA and numbers as well, but differently distributed.
What I am trying to do is to merge the two rows (or records) according to these two rules:
1) if both rows have a NA on a given field, I keep NA
2) if one of the two has a number, I use that value; if both of the rows contain the same value, I keep it also.
How do you do this without looping on each field of each row? (1M rows, tenths of fields, it will finish maybe tomorrow).
I do not know how to better explain my problem. I am sorry for the lengthy explaination, thanks a lot.
EDIT: it is better if I add an example. The following two lines
a,b,NA,NA,NA,1,2 ,NA
a,b,NA,3 ,NA,1,NA,NA
should become
a,b,NA,3 ,NA,1,2 ,NA

How do I select specific vectors of a matrix to be plotted against each other (such as when using hexplom)?

Is there a quick way to code for those specific vectors? Like I only want to use every 4th column in my matrix then plot the selected columns. I'm very new to R and have absolutely no idea what I'm doing. I know how to select a single vector and how to select a certain number in a row but that doesn't really help.
If you're looking to extract every 4th column from a matrix you can use seq().
Here's an example. I made a dummy dataset: foo<-matrix(c(rep(c(4,3,2,7),100)),nrow=10,ncol=10)
Then you can store the column indexes that you want from your matrix like so:
colsyouwant<-seq(from = 4, to = ncol(foo), by = 4)
from = whatever column you'd like to start from, in your case the 4th. Then you specify where you'd like to stop, so I used the ncol function to count how many columns are in the matrix. In this case my matrix isn't a multiple of 4 but it doesn't matter because seq stops before then. Then by=4 because you want to select every fourth column.
The colsyouwant now equals to 4 8. Simply use brackets and the name of your variable to get the columns you want out. foo[,colsyouwant]. Here the brackets just specify what part of the matrix I want as an output, it goes [rows,columns]. Since I want all the rows I leave that spot blank and then specify the rows using the colsyouwant variable, or in other words 4 8.

How to code a numeric field in r by a set of labels

I have a large data frame with around 190000 rows. The data frame has a label column storing 12 nominal categories. I want to change the weight column value of each row based on the label value of that row. For example, if the label of a row is "Res", I want to change its weight field value to 0.5 and if it is "Condo", I want to change its weight value to 2.
I know it is easy to implement this by if else statement but given the number of rows, the processing time takes so much long. I wanted to use cut() but it seems that cut categorizes based on intervals not nominal categories. I would appreciate any suggestion that can decrease the processing time.

extract columns that don't have a header or name in R

I need to extract the columns from a dataset without header names.
I have a ~10000 x 3 data set and I need to plot the first column against the second two.
I know how to do it when the columns have names ~ plot(data$V1, data$V2) but in this case they do not. How do I access each column individually when they do not have names?
Thanks
Why not give them sensible names?
names(data)=c("This","That","Other")
plot(data$This,data$That)
That's a better solution than using the column number, since names are meaningful and if your data changes to have a different number of columns your code may break in several places. Give your data the correct names and as long as you always refer to data$This then your code will work.
I usually select columns by their position in the matrix/data frame.
e.g.
dataset[,4] to select the 4th column.
The 1st number in brackets refers to rows, the second to columns. Here, I didn't use a "1st number" so all rows of column 4 are selected, i.e., the whole column.
This is easy to remember since it stems from matrix calculations. E.g., a 4x3 dimensional matrix has 4 rows and 3 columns. Thus when I want to select the 1st row of the third column, I could do something like matrix[1,3]

Resources