-1

I have the following dataframes in my workspace

a
b
c
d

In each of these dataframes I have a variable called visit which has several codes

212323 visit1
3343433 visit2
58854584 visit3

I want to loop over each of the dataframes and in each one I would like to label the values of the variable visit which is in each of the dataframes.

Please can you tell me how I do it in a way that is simple and concise?

Thanks

jww
  • 97,681
  • 90
  • 411
  • 885
Xavier
  • 55
  • 1
  • 1
  • 4
  • What do you mean by *"label the values"*? If you are dealing with multiple similarly/identically-structured frames, then I suggest you read about [lists-of-frames](http://stackoverflow.com/a/24376207/3358272). – r2evans Aug 31 '18 at 15:56
  • 2
    It would help if your question were a little more reproducible, including actual data (and any code you have tried so far). This might be as simple as building them statically (e.g., `data.frame(a=c(212323,...),b=c("visit1",...))`) or the output from `dput(head(a));dput(head(b));`. (You probably don't need more than two frames to demonstrate what you need.) Also please include your expected output. Refs: https://stackoverflow.com/questions/5963269/, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Aug 31 '18 at 16:08
  • 1
    As r2evans said, We need to see the actual data you're working with as well as an example of what your desired result is. I have no idea what you're asking – divibisan Aug 31 '18 at 16:09
  • I have 4 datasets called a, b, c and d. In each of them I have a variable called visit and I want to label the variables of this variable called visit. – Xavier Aug 31 '18 at 16:17
  • I want r to go to each dataset and label the variable visit – Xavier Aug 31 '18 at 16:18
  • visit has different variable 212323 , 3343433 , 58854584 and I want R to label the variable visit. For example, 212323 in "visit 1", etc... – Xavier Aug 31 '18 at 16:19
  • Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Sep 02 '18 at 17:17

1 Answers1

0

First change the variable in dataset a to a factor:

a$visit<-as.factor(a$visit)

then change the level of the factor to say visit 1 whenever it's value is equal to 212323:

 levels(a$visit)[levels(a$visit)=="212323"] <- "visit 1"

repeat the above code for all the other levels:

 levels(a$visit)[levels(a$visit)=="3343433 "] <- "visit 2"
 levels(a$visit)[levels(a$visit)=="58854584"] <- "visit 3"

repeat the code above in a loop or individually for the rest of your datasets (replace a with b, then c, then d) Please mark this as the correct answer if you get the results you wanted

Shirin Yavari
  • 626
  • 4
  • 6
  • Wow you are amazing. Thanks so much. Please could you tell me if there is a way to create a "program". For example, lets imagine this is my program called "visitlab" X$visitid<-as.factor(X$visitid) levels(X$visitid)[levels(X$visitid)=="32"] <- "visit 1" levels(X$visitid)[levels(X$visitid)=="33"] <- "visit 2" levels(X$visitid)[levels(X$visitid)=="35"] <- "visit 3", so what I would like to do is the following "visitlab a b c" . This authomatically would label the variable visitlab in each of the datasets a b c – Xavier Sep 02 '18 at 11:17
  • you are amazing. Thanks so much. Please could you tell me if there is a way to create a "program". For example, lets imagine this is my program called "visitlab" X$visitid<-as.factor(X$visitid) levels(X$visitid)[levels(X$visitid)=="32"] <- "visit 1" levels(X$visitid)[levels(X$visitid)=="33"] <- "visit 2" levels(X$visitid)[levels(X$visitid)=="35"] <- "visit 3", so what I would like to do is the following "visitlab a b c" . This would label the variable visitlab in each of the datasets a b c. Or,load the dataset a and write visitlab and the variable visitid gets labelled. – Xavier Sep 02 '18 at 11:25