I would like to know if it is possible, having the first table as a result of a transformation, build a matrix with the count of ok and nok, as shown in the second table
You need to unpivot your data; From this:
to this:
Use for that a build-in function from transform ribbon "Unpivot Column"
then add attributes to rows and value to column in matrix:
And this measure to Values:
Measure = CALCULATE(COUNTROWS(Sheet3), FILTER(ALL(Sheet3), SELECTEDVALUE(Sheet3[Attribute]) = Sheet3[Attribute] && SELECTEDVALUE(Sheet3[State]) = Sheet3[State]))
Related
I have a dataframe that looks something like the following.
KEYCODE= c("A","B","C","D")
values = list(c("12,3"),c("2"),c("2,7"),c("16"))
df = data.frame(KEYCODE=KEYCODE, values=cbind(values))
Now, I want to create a third column that will create values of 0 when the values field do not have anything between 7 to 18, else it will crate a value of 1. For instance,
My new df with the new column
Unfortunately, the elements in the values field are strings.
Thank in advance.
I want to make a vector for a loop with al values of a column.
Is this possible?
Currently I did it like this:
Client_id_conversion_vector <- as.vector(Test$dimension1)
dimension1 are the clientId's I want in my vector
The problem is that I get the same value over and over
You might be looking for the unique function:
Client_id_conversion_vector <- unique(Test$dimension1)
I have a dataframe (df) with a column of Latitude (Lat), and I need to match up the corresponding Longitude value (based off relationships in another dataset). New column name is 'Long_matched'.
Here, I am trying to write a new value in the column 'Long_matched' at the corresponding row to latitudes between -33.9238 and -33.9236. The data in 'Lat' has many more decimal places (e.g: -33.9238026666667, -33.9236026666667, etc.). As I will be applying this code to multiple datasets over the same geographical location (hence the long decimals will vary slightly), I want to write Longitude values which fall within a a 0.0002 degree range.
Some attempts of code I have tried include:
df$Long_matched <- ifelse(df$Lat< -33.9236 & df$Lat> -33.9238, 151.2279 , "N/A")
or
df$Long_matched[df$Lat< -33.9236 & df$Lat> -33.9238] <- 151.2279
I think I need to use a for loop to loop through the rows and an if statement, but struggling to figure this out - any help would be appreciated!
Resulting output should look something like this:
Lat Long_matched
-33.9238026666667 151.2279
-33.9236026666667 (new long value will go here)
Everything said in the comments applies, but here is a trick you can try:
In the following code, you will need to replace text with numbers.
Latitude_breaks <- seq(min_latitude, max_latitude, 0.0002) # you need to replace `min_latitude`, `max_latitude`, and `increment` with numbers
Longitude_values <- seq(first, last, increment) # you need to replace `first`, `last` and `increment` with numbers
df <- within(df, {
# make a categorical version of `Lat`
Lat_cat <- cut(Lat, Latitude_breaks)
Long_matched <- Longitude_values[Lat_cat]
})
A few notes:
the values between min_latitude and min_latitude + 1 will be assigned to the values of Longitude marked first.
The length of Latitude_beaks should be one more than the length of Longitude_values.
Values of Lat outside of Latitude_breaks will becomes NAs.
This works by exploiting a nice feature of factors - they are stored as integers. So we can use them to index another vector - in this case, Longitude_values.
I currently have two tables, endingStock and totalUsage, with each row in each table having a date entry. I want to iterate over the rows of each table, compare the date, and if the dates are the same, I want to extract the value of a variable unit value from both endingStock and totalUsage, and append endingstock$unitvalue / totalUsage$unitvalue to a third table, ratioTable.
Using a bunch of different answers on this site, I've tried setting ratioTable to NULL and building up the table row by row, using the following loop:
for (i in 1:nrow(endingStocks)) {
for (j in 1:nrow(useTotals)) {
if (endingStocks[i,]$valuedate == useTotals[j,]$valuedate) {
ratio = endingStocks[i,]$`unit value` / useTotals[j,]$`unit value`
newrow <- c(endingStocks[i,]$valuedate, ratio)
ratioTable <- rbind(ratioTable, newrow)
}
}
}
However, this seems to be skipping values. Each dataframe has over 200 entries that are roughly matched in terms of date, and so the resultant ratioTable should have the same order of entries, but instead only has 24.
1) Is there a way to effectively do this using vectorized operations?
2) Are there any glaring faults with my code?
I'm guessing without a reproducible example, but I think using merge will do what you want:
ratioTable = merge(endingStocks, totalUsage, by="valuedate")
You can then tidy your new table as required.
pretty simple question, but I can't find an answer.
Suppose I want to transform data frame column type from string to double, but column name is unknown at compile time, so I can't write like:
train <- transform(train, columnName = as.double(columnName))
but if I write smth like this
varKnownAtRuntime <- "columnName"
train <- transform(train, varKnownAtRuntime = as.double(varKnownAtRuntime))
It just appends column "varKnownAtRuntime" to my data frame, while I want to change type of train$columnName
transform is a non-standard-evaluating shortcut function. If you want to use column names stored in variables you'll need to use standard evaluation functions like [.
varKnownAtRuntime <- "columnName"
train[, varKnownAtRuntime] = as.double(train[, varKnownAtRuntime])