Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

r - How to retrieve label and value information for data in dataframe A from dataframe B

Edited (in order to be more precise): I have a csv file in which the label and value information is stored for each variable in dataframe A.
In the original dataframe A there are 250 variables. For demonstration purposes I have:

  1. Reduced dataframe A from 250 to 7 variables.
  2. Loaded the csv file with the information as a dataframe B and also reduced to 7 variables.

My specific question: How can I assign the information (e.g. label and value) in dataframe B per code to the variables in dataframe A. So far I can achieve my goal by doing this variable by variable.

I hope the question is now more specific. I have no idea whether I am completly wrong with my way of thinking. Would be grateful for any help.

my dataframe A:

structure(list(a = c(2L, 7L, 8L, 5L, 10L, 1L, 6L, 9L, 3L, 4L), 
               b = c("29.06.2016", "18.07.2016", "26.07.2016", "04.08.2016", "12.08.2016", "12.08.2016", "24.08.2016", "26.08.2016", "27.08.2016", "27.08.2016"), 
               c = c("A", "A", "B", "A", "C", "C", "B", "C","B", "C"), 
               d = c(4795L, 7242L, 2246L, 7914L, 9910L, 4279L,9174L, 8329L, 8310L, 4799L), 
               e = c(6L, 10L, 8L, 10L, 11L, 7L, 11L, 2L, 12L, 4L), 
f = c(1973L, 1933L, 1977L, 1969L, 1960L, 1950L, 1963L, 1967L, 1951L, 1970L), 
               g = c(2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L)), row.names = c(NA, -10L), class = "data.frame")

my dataframe B (info for labels and values):

structure(list(
  variable = c("a", "b", "c", "d", "e", "f", "g"), 
  class = c("number", "string", "string", "string", "number", "number", "number"), 
  label = c("AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG"), 
  values = c("", 
             "", 
             "", 
             "", 
             "@0@,@k.A.@,@1@,@Januar@,@2@,@Februar@,@3@,@M?rz@,@4@,@April@,@5@,@Mai@,@6@,@Juni@,@7@,@Juli@,@8@,@August@,@9@,@September@,@10@,@Oktober@,@11@,@November@,@12@,@Dezember@", 
             "", 
             "@0@,@k.A.@,@1@,@female@,@2@,@male@")), 
  row.names = c(NA, -7L), class = "data.frame")


desired output:

enter image description here

question from:https://stackoverflow.com/questions/65864202/how-to-retrieve-label-and-value-information-for-data-in-dataframe-a-from-datafra

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try the followings. However, it would be easier if you read in e and g as factors before hand. Then, you won't need to convert them using as.factor.

library(lubridate)

dfB$label
dfC <- setNames(dfA, dfB$label)

# use a random date to generate level
a <- month(ymd(210101) + months(0:11), label = TRUE)

dfC$EEEE <- as.factor(dfC$EEEE) 
levels(dfC$EEEE) <- a


dfC$GGGG <- as.factor(dfC$GGGG)
levels(dfC$GGGG) <- c("female", "male")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...