loop over changing the default numeric rownames to texts in R -


I am writing a loop to change the default numerical row names related to a matrix (in my case matrix X) Specified in the matrix column (column with the name "sdp" in the matrix "temp"). So my loop looks like this:

 For  (i 2008: 2013) {Temp = P [p $ year == i]] (sprintf ("x_% d", i) Data matrix (temp [c ("a", "b")))) rownames (get sprintf ("x_% d", i)) = temporary $ sdp}  
< P> However, I am receiving the following error message

  & gt; (Sprintf ("x_% d", i)) = = Temporary $ sdp: The goal of the assignment extends to non-expansion (sprinter ("x_% d", i)) = = Error in floating $ sdp rownames Language object  

This work:

  & gt; Rownames (x_2008) = Temporary $ sdp> "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "(" x-dd ", i)) [1] 11" 12 " "13" "14" "15" "16" "17" "18" "19" "20" "21" [22] "22" "23" "24" "25" "26" "27" "28 "" 32 "" 34 "" 35 "" 36 "" 37 "" 38 "" 39 "" 40 "" 41 "" 42 "[43]" 43 "" 44 "" 45 "" 46 "" 47 "" 48 "" 49 "" 50 "" 51 "" 52 "" 53 "" 54 "" 55 "& gt; Rownames (x_2008) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" " 15 "" 16 "" 17 "18" "19" "20" "21" [22] "22" "23" "24" "25" "26" "27" "28" "29" "30 "" "32" "34" "38" "39" "40" "41" "42" [43] "43" "44" "45" "46" " 47 "" 48 "" 50 "" 51 "" 52 "" 54 "" 54 "" 55 " 

This is never a good idea to use assign you may

  xx & lt; - lapply (split (p, p $ year), function (x) should use a list like {rownames (x) & lt; -x $ sdp; x [, - (1: 2)]}) < / Code> 

and then for each year you

  xx [["" 2008 "]]  

and so on When you have to update with assign then try to replace the actual value with the get with the actual problem was a good work example here. P>

  # sample data p & lt; -data.frame (year = representative (2008: 2010, each = 3), SDP = representative (letter [1: 3], 3), A = ranif (9), B = rainif (9)) 2008: 2010) {Temp & lt; - p [p $ year == i,] x  

but using Assign / Receive That's what you are not actually doing things "r ve"


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -