626 questions
1
vote
1
answer
76
views
How to create a new column using complex fomulas and variable outputs (i.e., recoding variables) in a Pandas DataFrame similarly to R's `case_when`?
Main Problem
Suppose I have the following Pandas DataFrame (code shared below):
Suppose further that I want to create a new column called NewVal according to the following rule (process often called &...
1
vote
1
answer
39
views
Recoding a series of variables with names of the form NAMEx where x is a numeric series
I have a series of variables of the form NAMEx to which I want to apply the same recoding scheme. (In stata I would do this using "recode NAME* (recode specifications)"
My inclination was to ...
0
votes
1
answer
63
views
Recoding of item files with if-loop in R [closed]
I´m a uni student who recently started learning to use statistical analysis code with R.
Unfortunately I´m not the best yet, and my prof. isn´t really willing to help. Which is why I´m hoping that ...
0
votes
1
answer
56
views
Replacing numbers with numbers in R?
I just have what seems like a simple list of numbers, that I'd like to recategorize.
The numbers are: 1, 2, 3, 4, 5, 6, 7
I'd like to recode such that 1,2 = 1, 5=2 and 3,4,6,7 = 3.
I've tried the ...
0
votes
1
answer
28
views
How to Automatically Dummy Code High Cardinality Variables in Python
I am working my way through the data engineer salary data set on Kaggle. The salary_currency column has the following value counts.
salary_currency
USD 13695
GBP 558
EUR 406
INR 51
CAD 49
....
2
votes
1
answer
102
views
Create dummy for missing values for variable in Python
I have the following dataframe in:
a
1 3
2 2
3 Nan
4 3
5 Nan
I need to recode this column so it looks like this:
df_miss_a
1 0
2 0
3 1
4 0
5 1
I've tried:
df_miss_a = np.where(df['a'] == 'Nan', ...
4
votes
2
answers
641
views
Recode values using case_match() with a char array
In the dplyr package, recode() has been superseded in favor of case_match(). Is there a way to use labels stored in, for example, char array to recode values using case_match()?
For example, with ...
0
votes
1
answer
138
views
Issue with reverse coding in R tidyr
This is my code:*
data %>%
mutate(r_stemster_3 = recode(stemster_3,
`1` = 7,
`2` = 6,
`3` = 5,
...
1
vote
3
answers
521
views
What is the easiest way to group/recode multiple categories into few categories?
I have a column with almost a 100 string categories that I would like to group/recode into fewer categories. I am trying to figure out the easiest way to do so, I thought about turning it into factor ...
2
votes
2
answers
85
views
Why aren't my conditional ifelse recodes returning the expected values?
I'm creating a new variable that uses conditional "ifelse" statements based on the values of several source variables.
The source variables are a survey question that changed names over ...
1
vote
3
answers
220
views
How to replace cells if they contain part of a string in R
I have columns with different ratings from 1-5 with descriptors next to the number. The format is "number dash descriptor", ex. "1 - very happy" or "5 - hungry". I want ...
1
vote
2
answers
86
views
Assignment in mutate a value from dynamic column name in R
Here is my dataframe:
dt <- data.frame(
session_id = c("17472631", "17472632", "17472633", "17472635", "17472636", "17472638"),
...
0
votes
1
answer
55
views
Create a vector with the value 0 instead of NULL when counting the numbers of TRUE
I'd like to count the numbers of TRUE of a variable for each category of another variable. Or more precisely the average number of TRUE for each category. Afterward, I'd like to create a vector.
The ...
0
votes
1
answer
52
views
Add new rows in a R dataframe
This is my dataframe
df <- data.frame(Var=c("Var1", "Var1","Var1","Var1","Var1","Var1"),
measure=c("M1","M1&...
0
votes
4
answers
93
views
Recode string in column based on the name of the column, looked up in another table
I have a df containing values for several ids and multiple variables. There is a reserve code, "no", the same code for all variables, that I would like to recode to different values based on ...
0
votes
1
answer
55
views
Recode multiple values in multiple columns with new values in R
I have data frame with 18 columns. Columns 2 to 13 include numeric values such as 0, 1, 2, 4 ... I want to recode them based on range into three categories:
if columns 2:13 are 0 -> 0
if columns 2:...
0
votes
3
answers
39
views
How to recode one column conditional on another
I currently have a dataframe in R with a variable for names ("Name") and one that notes whether an individual is female ("Female") (1, NA). A given individual likely has multiple ...
0
votes
1
answer
63
views
How to recode a string to a numeric value in Stata?
I am using the replace command to recode a string variable countrycode to a numeric variable ccode.
But the replace command has no effect. Stata gives no error messages, bt just says "0 changes ...
0
votes
2
answers
108
views
how to apply changes to existing data variables
I perform lots of data transformation and one of them makes me some troubles.
Let's assume that I have a dataset with variavles from v1 to v100, every one with numbers from 1 to 5. I want to recode/...
1
vote
1
answer
64
views
How to recode values in a column in R?
I have the following data, where I have calculated the tercile for each value in x:
dc <- data.frame(x = c(1, 2, 5, 6, 8, 9))
dc$tercile <- fabricatr::split_quantile(dc$x, 3)
x tercile
1 ...
1
vote
2
answers
295
views
SPSS: How to access values of variables, where the names of the variables I want are the values of another variable
In SPSS, I have a variable, CumulLast, which takes on values between 1 and 20. I have another family of variables, of the form Status.X, where X takes on values between 1 and 20 (e.g. Status.1, Status....
1
vote
2
answers
154
views
Is there a Python code in SPSS to RECODE a specific value in all numeric questions?
I am working in SPSS and I need to create syntax to recode all numeric variables but only in case of a specific value of another value.
For all numeric variables I need to recode the value 50 to ...
0
votes
1
answer
86
views
How to map more informative values onto fill argument of sjPlot::plot_model
Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot....
3
votes
2
answers
141
views
Recoding multiple factors using regexp
I have data from a survey, where several questions are in the format
"Do you think that [xxxxxxx]"
The possible answers to the questions are in the format
"I am certain that [xxxxxxx]&...
0
votes
2
answers
103
views
Row sums doesn't match when using case_when and rowSums in R
I made up this example df to make an example.
set.seed(357)
# Generate binary variables
var1 <- sample(c(0, 1, NA), 200, replace = TRUE, prob = c(0.4, 0.4, 0.2))
var2 <- sample(c(0, 1, NA), 200, ...
0
votes
2
answers
124
views
How to recode multiple columns with the same conditions?
I have a data set that has over 260 columns that have character values that need to be recoded as numerical factors. For example, N=0, Y=1, S=1, O=2, F=99. However, apply this to only some columns, ...
-1
votes
1
answer
172
views
Recode string variable to numeric variable
I have a variable in Stata which consists of letters such as A, B, AB, AC, etc.
I want to change to numeric, with values numbers instead of letters, such as 1 instead of A.
I tried to encode the ...
0
votes
0
answers
50
views
Releveling multiple factors with a common word
Apologies, pretty new to R and I'm struggling with releveling multiple variables that have a common element/word.
What I want to do is mutate all variables that have a common word in the same way.
All ...
0
votes
1
answer
933
views
How to merge multiple dataframes with a different number of rows by matching the ID in R [duplicate]
I'm sure that this answer exists somewhere but I've been trying to get this code to work and I just can't seem to be able to for my purpose. I have 7 different dataframes which each contains an ID, ...
1
vote
3
answers
128
views
How to make all responses in a column into their own unique column in R
I currently have a dataset in R that is in long format and I'm trying to make it wide with a couple of specifications. So my dataset has a respondent ID and their gender along with one other column (...
0
votes
1
answer
183
views
Recoding values in R
I am trying to make the table below show a "Y" if the cell is populated. And if the cell is empty it is recoded as an "N"
Below I have provided a image along with the code I am ...
1
vote
4
answers
1k
views
How to recode a variable based on the value of another variable?
Let's say I have this data frame.
df <- data.frame(record = c("1", "2", "3", "4", "5", "6"),
fruit = c("apple",...
2
votes
3
answers
1k
views
How to recode a range of values (numeric) into a factor variable?
I want to recode a variable that has values from 1 to 10 (numeric) into a factor variable of 5 values. I'm using the command dplyr::recode, but it is not working. I know how to do it with the cut ...
1
vote
3
answers
196
views
Renaming columns systematically
Consider the two dataframe df1, df2 that have different column names. I need to systematically change the column naming scheme to obtain the same as in df_target
df1 <- data.frame(
a = 0,
b = 0
...
0
votes
1
answer
218
views
Recoding same variable across multiple data frames
I want to create a simplified way of recoding the same variable (the same way) across multiple data frames. For example, right now I'm re-coding an age variable from state datasets FL and GA. I'm ...
1
vote
2
answers
121
views
Using vector for recoding variables in a dataframe
In a recent project, I have quite a big data frame.
And I'd like to reprogram certain variables using a vector that I defined earlier.
I know there are many other ways to recode the data, but I was ...
5
votes
2
answers
249
views
Using purrr to recode across multiple columns with multiple mappings
I have a dataframe with questionnaire response labels. I always like to make a tibble with item-answer definitions and then use dplyr::recode() to replace all item labels with their corresponding ...
1
vote
4
answers
150
views
Recoding continuous variable
I have a continuous variable. Entries 1-60 need to stay the same. NAs and 0s are coded as a number above 60.
I tried using mutate in a few different ways, but none worked. Most of the outcomes were ...
1
vote
1
answer
62
views
How to change the value in specific rows based on a given condition in R?
I hope I can explain this properly but essentially, I am trying to organize some data that I have that is censored. Say, for example I have people who are still alive ("."), people who died (...
2
votes
3
answers
97
views
How to make all values in a row after a specific value appears, NA in R
I have data in R that looks like this:
T1 <- c(0,0,0,0,0)
T2 <- c(1,0,0,0,0)
T3 <- c(0,1,0,0,0)
T4 <- c(1,1,0,NA,1)
T5 <- c(0,1,0,NA,0)
df <- data.frame(T1,T2,T3,T4,T5)
T1 T2 T3 ...
1
vote
1
answer
63
views
How to shift values left in R so that first non-NA value propagates column 1
I am trying to create a new set of variables based on observations at 5 different time points. However, there is not an observation for each row at each time point. Assuming it looks something like ...
1
vote
2
answers
112
views
Recode data frame based on an index column
I have the following data frame df:
Index col_a col_b col_c col_d
1 4 c v g j
2 1 x <NA> z s
3 1, 3 k j n y
4 2 q t o i
...
0
votes
1
answer
51
views
How to recode a new variable based on whether 'NA' appears across that row in R?
I feel like this may be a very easy fix but I can't seem to get it to work correctly, I'm sorry. Essentially, I am trying to create a variable that dichotomizes whether 'NA' appears across multiple ...
1
vote
0
answers
2k
views
How to use recode with dplyr properly
I am trying to recode multiple columns, using a code that has previously worked:
test <- tibble (
a_test = c(7,6,5,4),
b_test = c(7,7,5,3),
c_test = c(7,3,7,5),
d_test = c(7,7,7,7)
)
test %...
0
votes
1
answer
307
views
Error with dplyr::recode(): "Either all values must be named, or none must be named"
I'm trying to recode the values in my column but I keep getting errors. My code looks like this:
dataTable1 %>%
mutate(income = recode(income, "1" = "Under $20,000", "2&...
3
votes
2
answers
741
views
How can I create a day number variable in R based on dates?
I want to create a variable with the number of the day a participant took a survey (first day, second day, thirds day, etc.)
The issue is that there are participants that took the survey after ...
3
votes
4
answers
2k
views
How to count the number of occurrences of a given value for each row?
I'm sure this is a really easy fix but I can't seem to find the answer... I am trying to create a column at the end of my dataframe that is a sum of the number of times a specific value (say "1&...
0
votes
2
answers
544
views
How to change values across 1 row based on values in a column in R?
I have a lot of columns in 1 dataframe that identify different timepoints of the same variable. Basically, within my data, if there's no response at timepoint X-1, there will be no response at time ...
1
vote
1
answer
69
views
How to recode multiple columns that are characters into numeric at once?
I'm new here so apologies if I make a couple mistakes here but essentially, I am trying to recode a lot of columns that have to do with the same content but at different time points. Therefore, I'm ...
0
votes
1
answer
704
views
Recode numeric variables
I've searched several help-pages and already answered questions but still get an error message when I try to recode multiple columns.
Example dataset:
test <- tibble (
a_test = c(7,6,5,4),
...