0

I have a data frame, imported with excel with library(readxl). It contains long columns of data each with its own column title. Now I need to store specific values in new variables. I stored the column titles in the vector "titles" and want to extract certain values from a specific row e.g 151 and store it in a new variable.

I tried with the code below. I am really new to R and tried a lot and failed...

example <- data.frame(c('N 1','N 2'), c(50, 60), c(70, 80))
titles <- c('N 1', 'N 2')
for (i in titles) {
  (paste("nkorrigiert",i)) <- as.numeric(example[[paste(i)]][3])
}
dput(head(example))

and get this

Fehler in (paste("nkorrigiert", i)) <- as.numeric(example[[paste(i)]][3]) : Ziel der Zuweisung expandiert zu keinem Sprachobjekt

> dput(head(example))

structure(list(c..N.1....N.2.. = structure(1:2, .Label = c("N 1", 
"N 2"), class = "factor"), c.50..60. = c(50, 60), c.70..80. = c(70, 
80)), .Names = c("c..N.1....N.2..", "c.50..60.", "c.70..80."),
row.names = 1:2, class = "data.frame")

What am I doing wrong?

lmo
  • 37,904
  • 9
  • 56
  • 69
  • Can you give the error you get and also a reproducible example please ? – Orhan Yazar Aug 10 '17 at 13:27
  • Please type `dput(head(nDaten))` in the console and paste the output into your question. Your code is seriously wrong. First, if you want to create an object with `paste`, you will need to use `assign`. Second, your indexing is wrong. – LAP Aug 10 '17 at 13:30
  • 1
    Please read "An Introduction to R", specifically the section on assigning to subsets of data: https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Index-vectors – Nathan Werth Aug 10 '17 at 13:31
  • okey tried to make an example, i hope it helps you to help me :) I am going to read this this evening in the train, thanks for the tip! – Sebastian Ortmann Aug 10 '17 at 13:49
  • [This post](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names) is worth a read I suspect. – lmo Aug 10 '17 at 14:00
  • thanks @lmo, ill read it! – Sebastian Ortmann Aug 10 '17 at 14:11

2 Answers2

0

You can use the assign command

    example <- data.frame(c('N 1','N 2'), c(50, 60), c(70, 80))
titles <- c('N 1', 'N 2')
for (i in titles) {
  assign(paste("nkorrigiert",i),  as.numeric(example[[paste(i)]][3])) 
}
dput(head(example))

R does not understand that you want to create a new variable

George Sotiropoulos
  • 1,864
  • 1
  • 22
  • 32
  • thanks! i allready tried with the assign command. Your example still gives me numeric empty variables. e.g. > 'nkorrigiert N 1' [1] "nkorrigiert N 1" – Sebastian Ortmann Aug 10 '17 at 14:06
  • @Sebastian Ortmann I think thats because the `as.numeric(example[[paste(i)]][3])` is empty. The names of the columns are different when you create them in that way, and the index is wrong ( there are only two observations in each column) – George Sotiropoulos Aug 10 '17 at 14:23
0

With your help and the post suggested by @lmo i was able to solve it. Thank you guys! :D

Now i have my first code almost running, yay! With this code under the foor loop it was possible

as.numeric(assign(paste("nkorrigiert",i), example[3, i]))

Now i just need to find out how to calculate with this values stored in variable names in a for loop!:D

All the best, Sebastian