Questions tagged [rowwise]
229 questions
0
votes
1 answer
dplyr: repeating rows based on splitted factor
I was wondering if it is possible to use dplyr to repeat rows based on the result of a function.
If I have a data frame that looks like this:
df <- data.frame(count = 1:4, type = LETTERS[1:4], subtype = letters[1:4],
stringsAsFactors =…

Giovanni
- 121
- 1
- 1
- 11
0
votes
0 answers
row wise function in dplyr r
I am new to r and just trying the row wise function. But it seems did not really give me what I wanted.
df1 <- data.frame(Y1 = c(1, 2, 3, 4, 5),
Y2 = c(1, 3, 4, 2, 6),
Y3 = c(4, 1, 0, 0, 1))
df0 = df1 %>% rowwise() %>%…

Vin
- 19
- 2
0
votes
2 answers
How to add values rowwise in a grouped column
I have some sensor data with 100 data entries per second. In the last column are milliseconds, which for now are all 10. How can I rowwise sum the milliseconds together, grouped by time and date.
testdata <- structure(list(local_date =…

SeGa
- 9,454
- 3
- 31
- 70
0
votes
4 answers
Separate string column by row efficiently
I'm trying to separate a string column into two pieces based on chopping up the string. It's best illustrated with example below. rowwise does work, but given the size of the data.frame, I'd like to use a more efficient method. How can I avoid…

Lloyd Christmas
- 1,016
- 6
- 15
0
votes
0 answers
How can I calculate mean of columns row wise in pyspark?
I have a data frame of 4 numerical columns and I have to calculate mean of those columns and store the mean in another column in pyspark.
df["mean"] = df.loc[:,d_cols].apply(np.mean, axis=1) (python pandas)
I have to do the same thing as above but…

user3379108
- 9
- 3
0
votes
0 answers
How to conditionally do if/then statement row wise in r data frame
Suppose I have two data frame like these:
df1 <- data.frame(
customer = c('john','sally','bill','david','sam','jake'),
coupon_id = c('a',NA,'d',NA,NA,'c'),
final_price = c(100,50,40,25,100,200))
df2 <- data.frame(
coupon_id =…

Saul Feliz
- 668
- 3
- 11
- 20
0
votes
1 answer
How to find mean CTD profile from multiple CTD data files (row wise average of same variable in multiple data files) on python
I have had a difficult time trying to write this question. I have multiple CTD data files (files that contain ocean temperature values with depth). I have plotted them onto one figure to see how temperature changes with depth. What I would like to…

Mat
- 3
- 2
0
votes
0 answers
Extend a row with rowise() %>% do() when do() returns a vector
How do I make this code more tidy?
as_tibble(t(mapply(pmf, x=lhs$mu, y=lhs$sigma)))
Here pmf() is a helper function.
pmf <- function(x, y) diff(pnorm(c(-Inf, -2, 0, 2, Inf), x, y))
This code takes the mean and standard deviation from each row,
and…

Richard Herron
- 9,760
- 12
- 69
- 116
0
votes
1 answer
Dplyr rowwise not working on unnamed position identifiers
I'm trying to get the minimum time for each row in a dataframe. I don't know the names of the columns that I will be choosing, but I do know they will be the first to fifth columns:
data <- structure(list(Sch1 = c(99, 1903, 367),
…

pluke
- 3,832
- 5
- 45
- 68
0
votes
1 answer
Subset or filter data.frame per indices e.g. column-wise per row
Assume you have such a data.frame:
df <- data.frame(matrix(1:12, 4))
df
X1 X2 X3
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
which have to be filtered row-wise by these column indices:
b=c(2,1,3,2)
So the expected output should be this:
c(5, 2,…

Roman
- 17,008
- 3
- 36
- 49
0
votes
1 answer
rowwise() not working within function?
I'm new to R, and I'm trying to write a function that will add the entries
of a data frame column by row, and return the data frame with
a column of the new row of sums
that column named.
Here's a sample df of my data:
Ethnicity <- c('A', 'B',…

AndyDufresne
- 3
- 4
-1
votes
1 answer
R: applying custom function row by row with mutate()
I have created a function that uses st_join() from the sf package to extract the congressional district (a polygon) from a set of latitude and longitude coordinates, using a different shapefile to identify the congressional district depending on a…

RSS
- 163
- 1
- 2
- 11
-1
votes
2 answers
What are faster ways of reading big data set and apply row wise operations other than pandas and dask?
I am working on a code where I need to populate a set of data structure based on each row of a big table. Right now, I am using pandas to read the data and do some elementary data validation preprocess. However, when I get to the rest of process and…

Adrian
- 213
- 4
- 9
-1
votes
3 answers
R dplyr: Row wise operations using custom function
In pandas I frequently perform row wise operations with a custom function like this:
df = pd.DataFrame({'v1': [1, 2, 3], 'v2': [3, 4, 6], 'v3': [3, 4, 5]})
def f(row):
return(sum(row[["v1", "v3"]]) if row.v2 == 3 else 7)
df["new_col"] =…

Jonatan Pallesen
- 135
- 3
- 9
-1
votes
2 answers
Apply function over data frame rows
I'm trying to apply a function over the rows of a data frame and return a value based on the value of each element in a column. I'd prefer to pass the whole dataframe instead of naming each variable as the actual code has many variables - this is a…

Zeus
- 1,496
- 2
- 24
- 53