Replace Particular Value in Data Frame in R (2 Examples) | Change Certain Values in Variable/Column

  Рет қаралды 22,708

Statistics Globe

Statistics Globe

Күн бұрын

Пікірлер: 67
@gabgab8691
@gabgab8691 3 жыл бұрын
I have been searching for this kind of information for hours. Thank you so much for sharing!!
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
You are so welcome, glad it was helpful! :)
@RodBarkerdigitalmediablog
@RodBarkerdigitalmediablog 2 жыл бұрын
wow , thanks so much for your help with learning to change variables in a dataframe - I've been searching for about a week to overcome this problem, finally I can now sleep.
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
That's really great to hear Rod! Thanks for the kind words :)
@Pooh991
@Pooh991 3 жыл бұрын
Great video Joachim! Again, you really helped me :D I checked out your blog and really appreciate your content!
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Thanks a lot for the kind words Mateus! Great to hear that you like my content! :)
@puddy1999
@puddy1999 4 ай бұрын
Great way and just what I needed. Thanks!
@micha.statisticsglobe
@micha.statisticsglobe 4 ай бұрын
Thank you very much for your kind feedback!
@satadrubhattacharya784
@satadrubhattacharya784 4 жыл бұрын
I was just seeking this technique and here I got it. Thank you sir..
@StatisticsGlobe
@StatisticsGlobe 4 жыл бұрын
That's great Baban! Nice to hear that it helped :)
@multitaskprueba1
@multitaskprueba1 4 жыл бұрын
You are a genius and a great professor! Thanks!
@StatisticsGlobe
@StatisticsGlobe 4 жыл бұрын
Wow, thanks a lot for this awesome feedback! :)
@ziyabg1461
@ziyabg1461 4 жыл бұрын
Clearly explained differences between factors and characters which is case sensitive. Thanks!
@StatisticsGlobe
@StatisticsGlobe 4 жыл бұрын
Thanks for the nice comment Elton!
@mauri228
@mauri228 3 жыл бұрын
huge thanks sir!
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
You're very welcome, glad it helped!
@yanni3379
@yanni3379 2 жыл бұрын
Hi, would it be possible to change the values in a certain column by the range of the row for example rows 1:150 change to January.
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
Hey Yanni, you may accomplish this using the following code: data$x1[1:150]
@elisabblah1496
@elisabblah1496 3 жыл бұрын
This was so smple and helpful.
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Thank you Eli, glad it helped!
@mitchelldavidson2595
@mitchelldavidson2595 3 жыл бұрын
Love the accent
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Haha, thanks Mitchell. Or as we say in Germany - sanks :D
@franz2644
@franz2644 2 жыл бұрын
I have a question: I have a a loop and when the condition is true it should take a return value (which is numeric) for that day from xts1 an put it in my other xts object in that particular day. Could you please help?
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
Hey Franz, could you please provide some example data and illustrate how this data should look like at the end of the code?
@franz2644
@franz2644 2 жыл бұрын
@@StatisticsGlobe xts1 is a share with daily dates and a return column over many years. for ((i in 1:length(spread_jahr_1))){ #for all days if (i > trigger){ #if the value on that day is above the triggervalue #get the return value from xts1 on that day and add it in my other xts objekt/or dataframe at exactly the same date (and replace a 0 or NA value) } or is it smarter to do such a task with purr?
@franz2644
@franz2644 2 жыл бұрын
@@StatisticsGlobe for (i in 1:length(spread_jahr_1)){ #jeder tag if (spread_jahr_1$return >= 0){ assign spread_jahr_1$offene_Posi ==0 else assign spread_jahr_1$offene_Posi ==1 } } # but not assign, it should change the value in the xts objekt on that date to 0 or 1 deppending on the threshold
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
Hey Franz, are you looking for something like this? set.seed(354744) spread_jahr_1 = 0){ spread_jahr_1$offene_Posi[i]
@franz2644
@franz2644 2 жыл бұрын
@@StatisticsGlobe thank you very much. How could I do it easier?
@hijack42
@hijack42 2 жыл бұрын
Thank you!!!
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
You're very welcome Taynara!
@sidritkraja5258
@sidritkraja5258 Жыл бұрын
Do you also make Slingshot videos ? 🤔
@cansustatisticsglobe
@cansustatisticsglobe Жыл бұрын
Hello Sidrit, We will take your suggestion into account. At the moment, we do not have any videos on that topic. Regards, Cansu
@saisskumar1502
@saisskumar1502 3 жыл бұрын
Hi, I have a DF where the columns are of type char. I want to replace all the "x" in all the columns with 0 (zero). I tried following your code however it did not work because I am trying to convert char to int (x to 0). Which approach can be used in my case?
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Hey Sai, is it working when you put the zero in quotes? I.e. data[data == "x"]
@saisskumar1502
@saisskumar1502 3 жыл бұрын
@@StatisticsGlobe Hi, This is nice and new. I'll try the above mentioned suggestion. Meanwhile what I did was replaced the X values with NA and while plotting the dataset using ggplot, i used na.rm=TRUE which removed the 0s
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Hey Sai, nice to hear that it worked to replace "x" with NA. Now, you may replace these NA with 0 as described here: statisticsglobe.com/r-replace-na-with-0/ Also thanks for your email, I've just received it. Regards, Joachim
@saisskumar1502
@saisskumar1502 3 жыл бұрын
@@StatisticsGlobe you are welcome. Looking forward to your reply.😊
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Is your problem solved with the link above? :)
@abhijeetjahagirdar6117
@abhijeetjahagirdar6117 4 жыл бұрын
Hi Sir, I am facing error while replacing character values in month of bank_train data set to numeric 1-12 as instead of jan,feb,it is showing NA after executing the code it is not showing numbers. Following code in R i tried- month.num
@StatisticsGlobe
@StatisticsGlobe 4 жыл бұрын
Hey Abhijeet, please try to execute the following R code: bank_train$month
@abhijeetjahagirdar6117
@abhijeetjahagirdar6117 4 жыл бұрын
@@StatisticsGlobe yes Sir it really worked you are awesome Sir thanks for your assistance, if I get stuck in some prob can I ask here
@StatisticsGlobe
@StatisticsGlobe 4 жыл бұрын
Nice to hear that it helped! Sure, as long as the questions are not too complex/time consuming you can ask them here :) Regards, Joachim
@mr.marble476
@mr.marble476 3 жыл бұрын
Sir I want to change variables from character to numeric in only some of the columns of a data frame. how do I write a loop statement for that. I eas able to change it for 1 column, how can I repeat it without writing the same thing over and over again
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Hi, have a look at the second example of this tutorial: statisticsglobe.com/convert-data-frame-column-to-numeric-in-r Regards, Joachim
@mr.marble476
@mr.marble476 3 жыл бұрын
hey@@StatisticsGlobe, that was quite innovative and informative but doesn't help my case here. So basically I have values in form of characters already present in the columns which I want to replace with numbers 0, 1 in form of numeric. applying your aforesaid method all my character values are becoming NA in form of numeric, which I then can't change to case-specific 0,1
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
OK, got it. Are the character strings you want to replace in all columns the same?
@mr.marble476
@mr.marble476 3 жыл бұрын
@@StatisticsGlobe yes precisely
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
You don't need a for-loop for this. You can exchange the values in a subset of columns as shown in the following example: data[ , c("col1", "col3", "col7")][data[ , c("col1", "col3", "col7")] == "A"]
@isaacfritsch
@isaacfritsch 3 жыл бұрын
how to change a lot of values at once? i tryed make data2[data2 == c('A','B','C','D')]
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Hey Isaac, the following code would be a solution (not sure if it's the most efficient way though): data[data == "A" | data == "B" | data == "C" | data == "D"] <- "XXX" I hope that helps, Joachim
@isaacfritsch
@isaacfritsch 3 жыл бұрын
@@StatisticsGlobe i will try it, ty
@isaacfritsch
@isaacfritsch 3 жыл бұрын
@@StatisticsGlobe it worked ty
@StatisticsGlobe
@StatisticsGlobe 3 жыл бұрын
Great to hear Isaac!
@mahmoudmoustafamohammed5896
@mahmoudmoustafamohammed5896 2 жыл бұрын
Hey Joachim again, Thank you so much for this amazing videos. I have a small problem with applying the code on a variable in my data frame. in this variable I have a score value from 1-10 but there are 1 and -2 and -3 values for missing data and "I do not know" and no answer respectively . I wanted to delete them and replace them with NA or Zero -don't know honestly which is better but I thought simply NA and then remove the NA from the analysis. here is my code: Germany_2[Germany_2$Q164 == "-2"]
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
Hey Mahmoud, thank you very much for the wonderful feedback! Glad you find my videos useful! :) Regarding your question, does the following code work for you? Germany_2$Q164[Germany_2$Q164 == "-2"]
@mahmoudmoustafamohammed5896
@mahmoudmoustafamohammed5896 2 жыл бұрын
@@StatisticsGlobe Thank you soo much 😀 I just found it in other video of yours and I tried it immidiately and it worked perfectly. You are really amazing. Thank you so much 🤗
@StatisticsGlobe
@StatisticsGlobe 2 жыл бұрын
Thanks a lot Mahmoud, this is very kind! :)
MY HEIGHT vs MrBEAST CREW 🙈📏
00:22
Celine Dept
Рет қаралды 107 МЛН
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Will A Basketball Boat Hold My Weight?
00:30
MrBeast
Рет қаралды 142 МЛН
Splitting columns in R with the separate() command
5:02
Equitable Equations
Рет қаралды 11 М.
Replace values in data table based on conditions in R
1:53
ExplainHowToSimply
Рет қаралды 2,4 М.
R Tutorial 06: Extracting Values from Data Frames
9:02
Michael T. Gastner's Quantitative Reasoning
Рет қаралды 16 М.
Changing Variable Names in R
5:21
BridgeText
Рет қаралды 1,5 М.
Data wrangling with R in 27 minutes
27:19
Equitable Equations
Рет қаралды 27 М.
Replace characters  in data.frame in R
2:46
ExplainHowToSimply
Рет қаралды 2,1 М.
Learning R: 21 How to replace single and multiple values in R
5:29
Introduction to R: Dealing With Dates
12:36
DataDaft
Рет қаралды 37 М.
MY HEIGHT vs MrBEAST CREW 🙈📏
00:22
Celine Dept
Рет қаралды 107 МЛН